954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

reverse num help...

hi...i am worjig on project to display numbers entered by user..and display it in random order....i use devcpp..
and here is the code..it does compile but is not working properly..can anyone help me plz as quickly as possible..this is due within 4 days...

#include<iostream>
using namespace std;
int main(){
    int nums[14],checknum[14],displaynum[14],position=0,num;
    for(int a=0;a<15;a++){
    cout<<"enter value num #:"<<a<<endl;
    cin>>nums[a];
    checknum[a]=nums[a];
    
    }
   
    for (int b=0;b<15;){
                num=checknum[b];
                
        for(int c=0;c<15;){
        if(num>nums[c]){
             position++; 
             }c++;}//2nd for loop
             
             cout<<position<<endl;
             displaynum[position]=num; 
             b++; 
             position=0;}
     
        cout<<"Number are:";      
        for (int d=14;d>=0;)
        {cout<<displaynum[d]<<","; d--;} 
        system("pause");
        return(0);
        }
top123
Newbie Poster
4 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

For starters, you really need to work on your formatting, it's very hard to understand your code.

Second, this:

for(int a=0;a<15;a++){
    cout<<"enter value num #:"<<a<<endl;
    cin>>nums[a];
    checknum[a]=nums[a];
 
    }

is going to cause errors. You are running from 0-14, which attempts to store 15 elements. Since your arrays only have 14 elements you need to limit your range to 0-13. You have the same basic error on all 4 of your loops.

If you have an array of SIZE elements, your array indexes will be 0 thru (SIZE-1). If you try to use SIZE as an index, you will overrun the boundaries of the array and most likely cause issues.

Correct your loops, then see how things go.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

thnkx dude..but i think u r wrong...array0-14 stores 15 elements ..............1-14 stores 14 +array[0] stores a element.....

top123
Newbie Poster
4 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

int nums[14],checknum[14],displaynum[14] is how you've declared your arrays, with 14 total elements. Your loop goes 0,1,2,...,14 which is 15 elements.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

And I'm the second one to agree with FBody. Do you still think he's wrong?

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Why don't you play with the numbers as a string and then convert them back to an int or double.

photoyid
Newbie Poster
20 posts since Feb 2010
Reputation Points: 36
Solved Threads: 3
 
thnkx dude..but i think u r wrong...array0-14 stores 15 elements ..............1-14 stores 14 +array[0] stores a element.....

In some languages, SIZE specifies the last element's index, but it's not not the case in C and C++.
Observe:

'this example is valid in most BASIC dialects
Dim SIZE as const integer = 4
Dim myArray(SIZE) as integer
'generates indexes 0 thru SIZE
'i.e. indexes 0,1,2,3,4
//this example applies to all C++
const int SIZE = 4;
int myArray[SIZE];
//generates indexes 0 thru (SIZE-1)
//i.e. indexes 0,1,2,3
//notice: there is no index 4, attempting to access is an error


Read up on arrays , fix your loops, then give us an update.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

my code works now..but if i enter same number twice it doesnot.....i will have to fix that...but anyways thnx.............

top123
Newbie Poster
4 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You