G
gambler
Guest
hey people's
So i've written this program a recursive form of selection sort- I'm pretty new to C++ and well I've been getting these errors and can't seem to fix 'em!-Any help?
Thanks!!
#include<iostream>
#include<cstdlib>
#include<iomanip>
void PrintArray(int x[]);
void SelectSort(int x[], int n);
using std::cout;
int main()
{
const int n = 20;
int x[] = {n};
//Loops through all positions of the array and assigns each position of
//the array to a random number between 1 and 1000
for(int i = 0; i < 20; i++)
{
int x = (1+ rand() % 1000); //Line 25
}
PrintArray(x);
cout << "\nArray after it has gone through Selection Sort." ;
SelectSort(x,n);
PrintArray(x);
//Prints all the elements within the indices of the Array
void PrintArray (int x[])
{
for (int t = 0; t < 20 - 1; t++) //Line 43
{
cout << x[t] << " ";
}
}
//Finds the position of the largest element of the array.
void LargestElement(int x[], int n)
{
int Temp; //temporary storage for exchange of values
int J; //Is loop control variable and Array Subscript
int MaxIndex; //holds index of largest value so far
MaxIndex = n; //assume x[n] contains initial largest value
for(J = n - 1; J >= 0; J--)
{
if(x[J] > x[MaxIndex])
{
MaxIndex = J; //assumes x[J] contains larges value
}
if(MaxIndex != n) //Makes sure MaxIndex is subscript
{
x[n] = x[MaxIndex];
x[MaxIndex] = Temp;
}
}
}
void SelectSort(int x[], int n)
{
if(n > 0)
{
LargestElement(x, n); //Place Largest value in x[n]
SelectSort(x, n - 1); //Sort Array 0..n-1
}
}
} //Line 89
So there are the error's
it's line 43 that I can't seem to fix!
In function `int main()':
25: error: variable-sized object `x' may not be initialized
42: error: parse error before `{' token
43: error: `t' undeclared (first use this function)
43: error: (Each undeclared identifier is reported only once for each
ion it appears in.)
43: error: parse error before `)' token
At global scope:
89: error: parse error before `}' token
So i've written this program a recursive form of selection sort- I'm pretty new to C++ and well I've been getting these errors and can't seem to fix 'em!-Any help?
Thanks!!
#include<iostream>
#include<cstdlib>
#include<iomanip>
void PrintArray(int x[]);
void SelectSort(int x[], int n);
using std::cout;
int main()
{
const int n = 20;
int x[] = {n};
//Loops through all positions of the array and assigns each position of
//the array to a random number between 1 and 1000
for(int i = 0; i < 20; i++)
{
int x = (1+ rand() % 1000); //Line 25
}
PrintArray(x);
cout << "\nArray after it has gone through Selection Sort." ;
SelectSort(x,n);
PrintArray(x);
//Prints all the elements within the indices of the Array
void PrintArray (int x[])
{
for (int t = 0; t < 20 - 1; t++) //Line 43
{
cout << x[t] << " ";
}
}
//Finds the position of the largest element of the array.
void LargestElement(int x[], int n)
{
int Temp; //temporary storage for exchange of values
int J; //Is loop control variable and Array Subscript
int MaxIndex; //holds index of largest value so far
MaxIndex = n; //assume x[n] contains initial largest value
for(J = n - 1; J >= 0; J--)
{
if(x[J] > x[MaxIndex])
{
MaxIndex = J; //assumes x[J] contains larges value
}
if(MaxIndex != n) //Makes sure MaxIndex is subscript
{
x[n] = x[MaxIndex];
x[MaxIndex] = Temp;
}
}
}
void SelectSort(int x[], int n)
{
if(n > 0)
{
LargestElement(x, n); //Place Largest value in x[n]
SelectSort(x, n - 1); //Sort Array 0..n-1
}
}
} //Line 89
So there are the error's
it's line 43 that I can't seem to fix!
In function `int main()':
25: error: variable-sized object `x' may not be initialized
42: error: parse error before `{' token
43: error: `t' undeclared (first use this function)
43: error: (Each undeclared identifier is reported only once for each
ion it appears in.)
43: error: parse error before `)' token
At global scope:
89: error: parse error before `}' token