Troubleshooting

Hey
i have been working on a program and i could use a little help with it. The program is rather simple it takes 10 points of user input places it in an array sorts it and then gives output. the only problem is that it does not do that. It takes the 10 points of data and gives rather strange output. any help would be appreciated
Thanks
here is a copy of what i got so far

#include <stdio.h>
#include <iostream>
using namespace std;

void getdata ( double nData[], int nNumber)
{
int i;

i = 0;
do {

cout<< "Input data: ";
cin >> nData[i];

i++;
} while ( i < 10);

}

void sort( double x[], int nCount)
{
int nswap;
int i;
double ntemp;
do {
( i=0, nswap=0) ; i < (nCount -1) ; i++;
{
if ( x[i] > x [i+1])
{
ntemp = x[i];
x[i] = x[i+1];
x[i+1] = ntemp;
nswap= 1;
}
}
} while ( nswap != 0);

}

void print( double x[], double nCount)
{
int i;
for(i=0; i<nCount;i++);
{
printf("dData[%d] = %d\t", i, x[i]);
}
}

void main(void)
{
int nCount=10;
double dData [10];
getdata (dData, nCount);
sort( dData, nCount);

print( dData, nCount);

system ("PAUSE");

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <iostream>

//declare formal arguements here
void getdata ( double nData[], int nNumber);
void sort( double x[], int nCount);
void print( double x[], double nCount);

int main()//int main is standard c++ and the (void) is only really needed in c
{
    int nCount=10;
    double dData [10];
    getdata (dData, nCount);
    sort( dData, nCount);
    
    print( dData, nCount);
    
    //system ("PAUSE");do not use this 
    return 0; //you forgot this
}

//declare the explicit definition of functions here
void getdata ( double nData[], int nNumber)//nNumber not used here
{
    for(int i = 0; i < 10; i++){
        std::cout<< "Input data: ";
        std::cin >> nData[i];
    }
}

void sort( double x[], int nCount)//what was going on here? i rewrote this to sort it but you need 
                                  //work on how to sort because it was a mess
{
    for(int i = 0; i < nCount; i++){
        
        for(int j = i + 1; j < nCount; j++){
            
            if(x[i] < x[j]) swap(x[i], x[j]); 
        }
    }
    
}

void print( double x[], double nCount)
{
    for(int i = 0; i < nCount ; i++) //no ; after for loop AND declare variables only in the needed 
                                     //scope 
    {
        std::cout << "dData[" << i << "] = " << x[i] << '\n' ;//use cout 
    }
}
Last edited on
Topic archived. No new replies allowed.