Hello dudes, a beginner and need some help. I have a homework that wants me to have the user input a person's name and test scores. The program than sorts the test scores from lowest to highest along with the names that go with it, and to show it up neatly in a two column way. So far I'm having trouble trying to conceptualize the whole thing.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
usingnamespace std;
int n,i;
char students[];
int scores [];
int times;
int main()
{
cout << "How many students and test scores do you want to enter?";
cin >> i;
times = 0;
if ( i == 0)
return 0;
do{
cout << "Enter a Student Name and his test score: ";
cin >> n;
students[times] = n;
times++;
}while( times < i);
cout << setw(10) << "Student Name" <<
setw(20) << "Test Scores" << endl;
system ("PAUSE");
return 0;
}
This code is obviously awful and I'm not sure how to get the sorting going right.
Alright what I did was I created a structure to store the student's names and scores. Then I created a sort method where it checks if score 1 is less than score 2 and if so it returns true or false. Then I had the user input the amount of students he wants to add ( The names/scores). Then I created a vector of the structure and set it's size to the amount of students the user will be adding. After that The user inputs their name and score and after getting those it pushes them to the back of the vector. then I used the sort method ( vec.begin() , vec.end() , customSortMentionedEarlier) If you try and still need some more help let me know and I can post some code.
The output of my code is this btw:
Please enter the number of students.
> 3
Please enter a name.
> Giblit
Please enter a score.
> 100
Please enter a name.
> Gib
Please enter a score.
> 75
Please enter a name.
> Lit
Please enter a score.
> 94
Student 1 name: Gib
Student 1 score: 75
Student 2 name: Lit
Student 2 score: 94
Student 3 name: Giblit
Student 3 score: 100
Process returned 0 (0x0) execution time : 21.652 s
Press any key to continue.
Edit:
Seems we posted about the same time and it might be hard to sort then maybe try using vectors?
and a structure is like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
struct Info
{
std::string name;
unsignedint score;
};
int main( int argc , char* argv[] )
{
Info student[ AMOUNT OF STUDENTS ]; //if you know initial value you could put the name before
// the semi-colon a couple lines up eg } student[5];
student[ 0 ].name = "Fred";
student[ 0 ].score = 100;
//ect...
//or with a vector like this
std::vector< Info > student( AMOUNT OF STUDENTS );
student[ 0 ].name = "Fred";
student[ 0 ].score = 100;
}
Not sure what bubble sort is ill have to check into that and what table? And sure you can sort the score in order easy but will be hard to sort the name to match score without struct class or map
[edit]
Seems like it wouldn't be that bad actually to do bubble sort but seems like it would be a bad idea if you have something large to sort like a database but for something small like this it seems viable
when ever you change the score just change the name position at same time i'll try to come up with an example
[/edit]
[edit2]Yes bubble sorting does work. Give it a try and if you can not figure it out let me know and I'll try to help you ( since I just made the code for it )[/edit2]
Just show some code but that link I sent you should get the job done for you =]
oh and for the function I'm not sure why they did it with a vector and a reference you can just do student and score arrays and ps you should use std::string name not char name for array ( so you get the full name and not first letter. ) and you could call the function like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void bubbleSort( int *score , std::string *name , int students)
{
//bubble sort method
}
int main ()
{
int students = 0;
std::cout << "Enter amount of students" std::flush;
//blah blah get the amount of students and set value equal to that.
int score[ STUDENTS ];
std::string name[ STUDENTS ];
bubbleSort( score , name , students );
//then output the sorted arrays
}
Okay I haven't had much time to tinker around with it, but I'm having trouble before even getting to the sorting. I'm having trouble with the parallel array and having the array only be the size of however much the user inputs. This is what it looks like at the moment.
// Project 5.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
int i,n;
string name[100];
int testscores[100];
int times;
int main()
{
cout << "How many students and test scores do you want to enter? Entering 0 ends the program." << endl;
cin >> i;
if ( i == 0)
return 0;
for (i=0;i<=99;i++)
{
cout << "Please enter a student's name: ";
cin >> name[i];
cout << "Please enter his test score: ";
cin >> testscores[i];
}
cout << endl;
cout << "Name" << " " << "Test Score" << endl;
for (i=0;i<=99;i++){
cout << name[i] << " " << testscores[i] << endl;
}
system ("PAUSE");
return 0;
}
I'm trying to do a parallel array, and then later sort that parallel array together and have it display.
I suggest making use of your n variable by using it in place of I on line 19. Then, you can replace the 99's in your for loop with n.
Of course, you also should be checking for valid input, i.e. if n is less than 0 or greater than 100 and if n is not an integer at all.
Okay thanks I got the parallel arrays to work for only user input, but now on to trying to figure out how to bubble sort a parallel array I guess. Though apparently index sort should be better which we haven't learned in class but I found out about it through googling. Guess I'll try and watch a tutorial on how to do that.
// Project 5.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
int i,n;
string name[100];
int testscores[100];
int times;
int main()
{
cout << "How many students and test scores do you want to enter? " << endl << "Entering 0 ends the program." << endl;
cin >> n;
if ( n == 0)
return 0;
for (i=0;i<n;i++)
{
cout << "Please enter a student's name: ";
cin >> name[i];
cout << "Please enter his test score: ";
cin >> testscores[i];
}
cout << endl;
cout << "Name" << " " << "Test Score" << endl;
for (i=0;i<n;i++){
cout << name[i] << " " << testscores[i] << endl;
}
system ("PAUSE");
return 0;
}
noooooooooooooo.
Don't use global variables try this
and I would initialize the scores and names after you get the input of how many scores so you can do this
1 2
std::string name[ n ];
unsignedshort scoes[ n ];
That will make those arrays the same size as the amount of students. Also did you check out that link I sent earlier? It has the exact formula that will work if I remember correctly it was something like
By global variables you mean declaring the i and n for example before the main program? Also if I try to do string name [n]; it gives me an error and says "Error: expression must have a constant value". And yeah I did look at the link but I couldn't make sense of it (sorry I'm slow :/ ).
Any variable declared outside of any functions, classes, structs, etc. are in the global namespace. This results in the chance that your variables might be modified without permission or incorrectly because any function can "see" them.
The error you got was from n not being a constant variable. You can have constint MAXSIZE in place of your 100's and initialize that variable with 100. This will allow you to make just one change to modify all the array sizes. I believe that variable array sizes on the stack will be possible in C++14.
Okay, I got the index sort to work but I don't know why it keeps doing this:
Your entered names and test scores unsorted:
Name Test Score
April 87
Joseph 85
Randy 100
This is your inputted names and test scores sorted by lowest to highest test sco
re:
Name Test Score
Joseph 85
This is your inputted names and test scores sorted by lowest to highest test sco
re:
Name Test Score
April 87
This is your inputted names and test scores sorted by lowest to highest test sco
re:
Name Test Score
Randy 100
Press any key to continue . . .
So what does your final code look like? because i have a similar project and im having trouble with looping the sorted numbers and names
Edit: also i dont understand what you mean by putting the for loop after the name and test score, and how that would change the program. i think i understand the rest tho