I am getting a really annoying error telling me that I need a ";" where I shouldn't.
The exact error is: test.cpp:38:3: expected ';' before 'median'.
What am I doing wrong?
#include <iostream>
usingnamespace std;
int main()
{
int n;
int i;
int sum;
int median;
int numArray;
int flag = 0;
int array[100];
do
{
cout << "\nPlease enter a number: ";
cin >> n;
numArray++;
if (n < 0)
flag = 1;
elseif (numArray > 100)
{
cout << "Too many entry numbers! Please try again.";
return 0;
}
else
{
array[i] = n;
sum += n;
i++;
}
}
while (flag == 0)
median = numArray/2;
while (1)
{
flag = 0;
for (i = 0; i + median < numArray+1; i++)
{
if (array[i] < array[i + median])
{
n = array[i + median];
array[i+median] = array[i];
array[i] = n;
flag = 1;
}
}
if (flag == 0)
{
if (median <= 1)
break;
else
median = median / 2;
}
}
cout << "The sorted array is " << array[i];
cout << "The number of entries is " << numArray - 1;
cout << "The median is " << array[median];
return 0;
}
Ooh okay, I think that fixed the segfault. Thank you!!!
however, now I'm having something else weird happening. The program stops after only one number is entered, it's supposed to only stop when over 100 numbers have been entered or when a negative is entered. It's also giving me really weird outputs.
#include <iostream>
usingnamespace std;
int main()
{
int temp;
int i = 1;
int sum = 0;
int median;
int numArray = 0;
int flag = 0;
int array[100];
do
{
cout << "\nPlease enter a number: ";
cin >> temp;
numArray++;
if (temp < 0)
flag = 1;
elseif (numArray > 100)
{
cout << "Too many entry numbers! Please try again.";
return 0;
}
else
{
array[i] = temp;
sum += temp;
i++;
}
}
while (flag = 0);
median = numArray/2;
while (1)
{
flag = 0;
for (i = 0; i + median < numArray+1; i++)
{
if (array[i] < array[i + median])
{
temp = array[i + median];
array[i+median] = array[i];
array[i] = temp;
flag = 1;
}
}
if (flag == 0)
{
if (median <= 1)
break;
else
median = median / 2;
}
}
cout << "\nThe sorted array is " << array[i];
cout << "\nThe number of entries is " << numArray - 1;
cout << "\nThe median is " << array[median];
return 0;
}
#include <iostream>
usingnamespace std;
int main()
{
int array[100];
int temp, index = 0;
while (1)
{
cout << "Enter a number: ";
cin >> temp;
if (temp < 0) // are we a minus?
break;
else {
array[index++] = temp;
if (index > 99) // array[100] is 0-99
break;
}
}
// index now has number of entries in array, lets
// now sort them in ascending order.
for (int i = 0; i < index; i++)
for (int j = 0; j < index - i - 1; j++)
if (array[j] > array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
cout << endl << "Numbers now sorted..." << endl;
for (int i = 0; i < index; i++)
cout << array[i] << endl;
return 0;
}
Enter a number: 22
Enter a number: 1
Enter a number: 5
Enter a number: 2
Enter a number: 7
Enter a number: 6
Enter a number: 4
Enter a number: 21
Enter a number: 11
Enter a number: 9
Enter a number: -1
Numbers now sorted...
1
2
4
5
6
7
9
11
21
22