Basically I had to make an array with values from user input. After I have the array I need to sort it. I am pretty sure I successfully sorted it... but I can't get it to print sorted. I get an error popup that the console has stopped working. I am new to cpp obviously.
// Unit 9 modularized.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<string>
usingnamespace std;
// Declare functions.
int askStudents();
int* askMovies(int getStudents);
int* sortMovies(int* askMovies);
//Declare variables.
int getStudents;
int* setMovies;
int main()
{
getStudents = askStudents();
askMovies(getStudents);
sortMovies(setMovies);
return 0;
}
int askStudents()
{
cout << "How many students were surveyed? ";
cin >> getStudents;
while (getStudents < 0)
{
cout << "Please enter a non-negative number: ";
cin >> getStudents;
}
return getStudents;
}
int* askMovies(int getStudents)
{
// Creating array "setMovies" with size of getStudents using pointer.
int *setMovies;
setMovies = newint[getStudents];
// Storing the amount of movies each student watched into setMovies array.
for (int i = 0; i < getStudents; i++)
{
cout << "How many movies did each student watch: ";
cin >> setMovies[i];
while (setMovies[i] < 0)
{
cout << "Please enter a non-negative number: ";
cin >> setMovies[i];
}
}
// Printint setMovies for test
for (int i = 0; i < getStudents; i++)
cout << setMovies[i];
return setMovies;
}
int* sortMovies(int* setMovies)
{
for (int i = 0; i < getStudents; i++)
{
for (int j = i + 1; j <getStudents; j++)
{
if (setMovies[i]>setMovies[j])
{
int temp = setMovies[i];
setMovies[i] = setMovies[j];
setMovies[j] = temp;
}
}
}
for (int i = 0; i < getStudents; i++)
cout << setMovies[i];
return setMovies;
}