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);
        }

Recommended Answers

All 7 Replies

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.

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

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.

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

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

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.