I am still a beginner in c++ programming and I have already tried searching, but another problem arises.
So I want the user to input the student's name and 4 quizScore. I want the names I inputted to be sorted alphabetically, and I still haven't solved that yet.
Furthermore, another problem arises: if I sort my variable names, the quiz score will not be sorted the way my name variables were arranged.
So basically, if I sort my variable name, I want the array quizeScores to be arranged in the same element as how the user inputted the data. I really need your help. I know my code is not the best and do love to have it revised. Thank you so much. Please help me...
#include <iostream>
#include <string>
using namespace std;
struct studen_grades {
string name;
int studentQuiz [1][4];
}records[5];
int main () {
double average = 0.0;
int x = 0, y = 0, recountFlag = 0, flag = 0;
bool repeat = true;
char addStudent = 'y';
string tempt;
cout << "Enter the number of students to input : ";
cin >> flag;
//input
for (x = 0; x < flag; x++) {
cout << "Enter student name (all-cap) : ";
cin >> records[x].name;
for (y = 0; y < 4; y++) {
cout << "Enter score in quiz " << y + 1 << " : ";
cin >> records[x].studentQuiz[x][y];
}
}
//sort the variable alphabetically
//Display the data
cout << "=============================================================================" << endl;
printf ("%-40s%-10s%-10s%-10s%-10s\n", "Student Name", "Quiz1", "Quiz2", "Quiz3", "Quiz4");
cout << "=============================================================================" << endl;
for (x = 0; x < flag; x++) {
printf ("%-40s", records[x].name.c_str());
for (y = 0; y < 4; y++) {
printf ("%-10d", records[x].studentQuiz[x][y]);
}
cout << "\n";
}
}
ask yourself (or you rubber duck https://en.wikipedia.org/wiki/Rubber_duck_debugging)
"if this struct holds the information for one student why is the studentQuiz a 2d array?"
struct studen_grades {
string name;
int studentQuiz [1][4]; <<<===?
}records[5];
surely you mean
struct studen_grades {
string name;
int studentQuiz [4]; <<<===
}records[5];
Now you can see why this blows up
for (x = 0; x < flag; x++) {
printf ("%-40s", records[x].name.c_str());
for (y = 0; y < 4; y++) {
printf ("%-10d", records[x].studentQuiz[x][y]); <<== x is > 0
}
cout << "\n";
}
should be
for (x = 0; x < flag; x++) {
printf ("%-40s", records[x].name.c_str());
for (y = 0; y < 4; y++) {
printf ("%-10d", records[x].studentQuiz[y]);
}
cout << "\n";
}
and here too
cin >> records[x].studentQuiz[x][y];
-----------------------------+++
Also this is c++ code why arent you using cout instead of printf
Of course there is no sort here. But at least you now have code that loads the data and prints it correctly.
Finally, note that you should really be using std::vector for the students rather than a fixed array
Related
I want to display an inverted half-star pyramid pattern next to each other.Here's the desired output
And here's my code:
#include <iostream>
using namespace std;
int main()
{
int n, x, y, k;
cout << "Enter Number of Rows: ";
cin >> n;
for (x = n; x >= 1; x--)
{
for (y = 1; y <= x; y++)
{
if (y <= x)
cout << "*";
else
cout << " ";
}
for (y = n; y >= 1; y--)
{
if (y <= x)
cout << "*";
else
cout << " ";
}
cout << "\n";
}
return 0;
}
Here's the output I got after running the code.
The number of rows desired is 10.
After running my code, the output isn't like what I expected. Please tell me how to make it right.
Thank you.
I saw some symmetries in the problem
for n rows, we're printing 2*n+1 characters
for the yth row, we're printing an asterisk if x is less than n-y or more than n+y
So I coded a single double loop with the more complex if statement. I had to adjusted the if statement until it worked.
#include <iostream>
using namespace std;
int main()
{
int n, x, y;
cout << "Enter Number of Rows: ";
cin >> n;
for (y = 0; y < n; y++)
{
for (x = 2*n+1; x > 0; x--)
{
if ((x > n+y+1) || (x < n-y+1))
cout << "*";
else
cout << " ";
}
cout << "\n";
}
return 0;
}
Well, you need to change your logic a little bit rest all is fine.
Here is the code:-
#include <iostream>
using namespace std;
int main()
{
int n, x, y, k;
cout << "Enter the number of Rows: ";
cin >> n;
for(x = 1; x <= n; x++)
{
for(y = n; y >= 1; y--)
{
if(y <= x)
cout << " ";
else
cout << "*";
}
for(y = 1; y <= n; y++)
{
if(y <= x)
cout << " ";
else
cout << "*";
}
cout << "\n";
}
return 0;
}
Explanation:- I am starting from row 1 and going till row "n". Now I need to print two different inverted flags so I used two for loops for that. In one loop I am starting from column "n" and going till row >1 and in the other loop, I am doing the just opposite of that so that both flags will be opposite to each other. Just try to understand this code by taking X as row and Y as column.
I was trying to write a code that takes two numbers as a input and change the numbers into words in certain rules.
Below is the code I wrote at first, but whatever input I put in, the loop starts from x=0.
#include <iostream>
int main() {
string nums[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
int x,y;
cin >> x;
cin >> y;
for (int x = x; x <= y; x++){
if (x <= 9){
cout << nums[x] << "\n";
}
else if (x % 2 == 0){
cout << "even" << "\n";
}
else {
cout << "odd" <<"\n";
}
}
return 0;
}
Below is the second code I wrote and it worked as I wanted to.
#include <iostream>
int main() {
string nums[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
int x,y;
cin >> x;
cin >> y;
for (int i = x; i <= y; i++){
if (i <= 9){
cout << nums[i] << "\n";
}
else if (i % 2 == 0){
cout << "even" << "\n";
}
else {
cout << "odd" <<"\n";
}
}
return 0;
}
I found out that if I add a variable, it works as what I wanted too. I found the solution but I don't know why I have to add a variable and why the first one always starts from x=0.
In first sample that you provided you defined a local variable(x) as same name in outer block:
for (int x = x; x <= y; x++)
In fact shadowing of variable take place here and c++ hide the declaration of variable of outer block with same name in nested block.
I have a 2 part question. I am showing a user defined array. User input is # of rows, columns, and at what interval in the array to put a symbol.
I cannot get an output of the default symbol ****.
Where do I need to insert the interval input to adjust the array accordingly? Inside the function itself or define another function to do this?
I am looking for this result. Input = 1 (row), 4 (columns), 2 (interval). Output would be **?*.
Here is what I have so far.
#include <iostream>
using namespace std;
int rows = 0, columns = 0, interval = 0;
char symbol;
void Display(int rows = 0, int columns = 0, int interval = 0, char symbol = ('*'));
int main()
{
cout << "Enter number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> columns;
cout << "Enter the number of the question mark interval: ";
cin >> interval;
cout << "\n";
cout << "How many rows do you want? " << rows << "\n";
cout << "How many columns do you want? " << columns << "\n";
cout << "How far between question marks? " << interval << "\n";
Display(rows, columns, interval, symbol);
return 0;
}
void Display(int rows, int columns, int intervals, char symbol)
{
for (int y = 1; y <= rows; y++)
{
for (int x = 1; x <= columns; x++) {
cout << symbol;
}
cout << endl;
}
system("pause");
}
The problem is you never assigned * to symbol.
Change
char symbol;
to
char symbol = '*';
Did you know about the disadvantages of global variables. The sooner you learn about the cons the better. Here is a starting point.
Modify the Display function as below:
void Display(int rows, int columns, int intervals, char symbol)
{
for (int y = 1; y <= rows; y++)
{
for (int x = 1; x <= columns; x++) {
if ((x % (interval + 1)) == 0) //<--since you want after every intervals, just do a mod operation
cout << "?";
else
cout << symbol;
}
cout << endl;
}
system("pause");
}
Here is the working example.
Okay so I am a bit stuck here with an assignment and really have no idea what to do next. The user is suppose input a series of key answers (A-D) and then have multiple students (the amount depends on the users input) input their own answers and the program should compare their answers with the answer key that was previously entered and score each student individually. Now my problem is, how do compare each student's answer with the answer key in the array? Here is my code so far
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int NumOfQ;
int NumOfS;
char TestAns[50];
string StudentNames[50];
int StudentScores[50];
char StudentAns[50];
int score[50];
cout << "Please enter the Number Of Questions\n";
cin >> NumOfQ;
cout <<"\nPlease enter your answers\n";
for(int x = 0; x < NumOfQ; x++){
cin >> TestAns[x];
}
cout <<"\nPlease enter number of students\n";
cin >> NumOfS;
cout <<"\nPlease enter Student's Names\n";
for (int s = 0; s < NumOfS; s++){
cin >> StudentNames[s];
}
for (int z = 0; z < NumOfS; z++){
cout<<"\nStudent: " << StudentNames[z] << " Please enter your test answers\n";
for (int a = 0; a < NumOfQ; a++){
cin >> StudentAns[a];
}
score[z] = 0;
for (int i = 0; i <=NumOfQ; i++){
if(TestAns[i] == StudentAns[i]){
score[z]++;
}
}
}
for(int Z = 0; Z < NumOfS; Z++){
cout <<"\Student : " << StudentNames[Z] << score[Z] << " out of " << NumOfQ << endl;
}
system("PAUSE");
return 0;
}
Scoring is very easy - you add one for every match in the answer and the key, like this:
int score = 0;
for (int i = 0 ; i != NumOfQ ; i++) {
if (TestAns[i] == StudentAns[i]) {
score++;
}
}
You can do it with even less code if you recall that logical operations return zero for "true" and one for "false".
WARNING: This may cost you some readability points among your peers, or even some real points if your professor fails to get impressed by such brevity:
int score = 0;
for (int i = 0 ; i != NumOfQ ; i++) {
score += (TestAns[i] == StudentAns[i]);
}
Case sensitivity is another concern: what if the answer key is c, and the user enters C? The above code would not give the student his hard-earned points, which is wrong. If you force the case of both characters to upper, mixed-case comparison would be correct:
if (toupper(TestAns[i]) == toupper(StudentAns[i])) {
score++;
}
Replace your last 'for' block with:
StudentScores[z] = 0;
for (int a = 0; a < NumOfQ; a++){
cin >> StudentAns[a];
if (TestAns[a] == StudentAns[a])
++StudentScores[z];
}
I am a bit stuck with my program, The program is to Read in a set of results and then construct a histogram that indicates how many marks are in each decade i.e how many between 10-20 etc.
I have 2 problems
How can I edit my code to only allow the readExamResults to store ones in the range of 0 -100
How can I get the PrintHisto to print the * at a certain setw() dependant on which decade each of the results where in. e.g. If I entered 3,4,5,11,23 It should show 3 * in the <10 decade, 1 in the 10-20 decade and 1 in the 20-30 decade.
any help will be much appreciated.
code:
using namespace std;
void readExamMarks(int examMarks[], int sizeOfArray){
cout << "Please enter a set of exam marks to see a histogram for:" << endl;
int x = 0;
for( int idx = 0; idx < sizeOfArray; idx++){
cin >> x;
examMarks[idx] = x;
}
}
void printExamMarks(int examMarks[], int sizeOfArray){
for(int x = 0; x < sizeOfArray; x++){
cout << setw(5) << examMarks[x];
}
cout << endl;
}
void printHisto(int examMarks[], int sizeOfArray){
system("cls");
for( int x = 0; x < 6; x++){
cout << setw(5) << "*" << endl;
}
}
int main()
{
int examMarks[5];
readExamMarks(examMarks, 5);
printHisto(examMarks, 5);
printExamMarks(examMarks,5);
system("PAUSE");
}
How can I edit my code to only allow the readExamResults to store ones
in the range of 0 -100
I guess you meant readExamMarks rather than readExamResults. If so, you would just need to add an if statement to check that the input value is actuall in the range [0..100]. I have also changed your loop statement for to a while because you just want to increase idx when a valid number is entered.
void readExamMarks(int examMarks[], int sizeOfArray)
{
cout << "Please enter a set of exam marks to see a histogram for:" << endl;
int x = 0;
int idx = 0;
while(idx < sizeOfarray)
cin >> x;
if((x >=0) && (x <= 100)){
examMarks[idx] = x;
idx++;
}
else
cout << "ERROR: Value must be in range [0...100], please enter a valid value\n";
}
}
Basically you create a new array with an entry for each decade. Then iterate over the exam grades and increase the according decade in the histogram:
vector<int> calculateHistogram(vector<int> grades)
{
vector<int> histogram(10, 0);
for (int i=0; i<grades.size(); i++) {
int decade = grades[i]/10;
histogram[decade] += 1;
}
return histogram;
}