Nov 22, 2012 at 8:59pm UTC
I have an assignment to do for tomorrow:
•Write a program that:
•1) Creates an array containing 100 random double numbers in the [0,250) range;
•2) Sorts them in descending order;
•3) Prints the contents of the sorted vector out. The sorting should be coded as a separate function double* sort_array(double* ).
This is my attempt:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std ;
double n ;
double nums ;
double* sort_array(double * nums)
{ int size=100;
int a, b, t;
for (a=1; a<size; a++)
{
for(b=size-1; b>=a; b--)
{
if(nums[b-1] > nums[b])
{
t = nums[b-1];
nums[b-1] = nums[b];
nums[b] = t;
}
}
}
return nums ;
}
int main ()
{
double rand_num[100] ;
int t ;
srand((unsigned)time(NULL)) ;
cout << "Here are 100 unsorted random numbers: \n " << endl ;
for (t=1; t<=100; ++t) {
rand_num[t] = (double)(rand()%250);
cout << rand_num[t] << " " ;
}
cout << "\n" << endl ;
sort_array[nums] ;
for (t =1 ; t <= 100 ; t++) {
cout << rand_num[t] ;
}
cout << "\n" << endl ;
cout << endl ;
return 0 ;
}
I get an error message for line 48 saying:
"Invalid types 'double*(double*)[double]' for array subscript".
Any help on the problem is appreciated!!
Nov 22, 2012 at 9:16pm UTC
You used [] which is for array indexing. For a function call you have to use ().
Also, the argument should be rand_num and not nums.
And your loops in main() should go from 0 to 99, not 1 to 100.