I've created this program and it is wrong. It is written for this assignment.
(Sorting) Read a set of numerical grades from the keyboard into an array named grades. The maximum number of grades to be entered is 50, so the size of the array should be 50 elements, and data entry should be terminated when a negative number is entered. Have your program invoke a sort() function to sort the grades and then display the sorted grades in ascending order (lowest to highest). You are free to use any of the sort functions given in the textbook. Remember to declare your sort() function with a function prototype.
This is the code and the error occurs on lines 18 and 21:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
void sort(int grades[], int size);
int main()
{
int size;
int grades[50], flag = 0, num;
int i, count = 0, temp;
cout << "Please enter the grades to sort: " << endl;
for (i = 0; (i < size && flag != 1); i++)
{
cin >> size;
if (num < 0){
cout << "You entered a negative number." << "The program will now end." << endl;
flag = 1;
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
usingnamespace std;
void sort(int grades[], int size);
int main()
{
int size;
int grades[50], flag = 0, num;
int i, count = 0, temp;
cout << "Please enter the grades to sort: " << endl;
for (i = 0; (i < size && flag != 1); i++) // size is uninitialized
{
cin >> size; // you overwrite the size
if (num < 0){ // but check num, which is uninitialized
cout << "You entered a negative number." << "The program will now end." << endl;
flag = 1;
cin.ignore();
}
else
{
grades[i] = num;
count++; // why both 'i' and 'count'?
}
sort(grades, count); // you sort and show result inside the loop, before all data is in array
for (int i = 0; i< size; i++)
cout << grades[i] << " ";
}
cout << endl;
cin.get();
return 0;
}
void sort(int grades[], int size)
{
int i, j, temp;
for (i = 0; i < size; i++)
{
for (j = 0; j < size - i - 1; j++)
{
if (grades[j] > grades[j + 1])
{
temp = grades[j];
grades[j] = grades[j + 1];
grades[j + 1] = temp;
}
}
}
}