I'm new to coding. I wrote the below code in C++ and I am not allow to use array.
You will create a console C++ program that uses a nested loop to enter each archer's individual end scores and then displays the total score for each archer.
I am stuck at how to calculate the total end score:
#include <iomanip>
using namespace std;
int main()
{
int Rounds = 4;
int Archers = 3;
int endScore ;
int average;
for (int a = 1; a <= Archers ; a++)
{
cout << endl << "Number " << a << " score" << endl;
int tEndScore = 0 ;
for(int i=1; i <=Rounds ; i++)
{
cout << "Round " << i << " : " ;
cin >> endScore;
while(cin.fail())
{
cout << endl << "not enter an integer " << endl ;
cout << "Please enter an integer ";
cin >> endScore;
}
tEndScore += endScore;
}
cout << endl << "The total score for 4 ends of Archer Number " << a << " is " << tEndScore << endl;
average =(double) tEndScore/Rounds;
cout << setiosflags(ios::fixed) << setprecision(2) << endl << "The average score of 4 ends of Archer Number " << a << " is " << average << endl;
}
}
This is the result after running. It will only use the last value I entered as tEndScore:
You need to shift tEndScore =+ endScore; this line inside the second for loop as
for(int i=1; i <=Rounds ; i++)
{
...
...
tEndScore += endScore;
}
And it will be a good practice (And mandatory for your code...) to initialize the tEndScore for each player as
for (int a = 1; a <= Archers ; a++)
{
tEndScore = 0;
endScore = 0;
average = 0;
...
...
}
You need to replace totalEndScore to tEndScore and totalRounds to Rounds.
Related
I am trying to add up all the values that have been stored into array b and have it display under the "total column" and don't know how to only have the scores add together.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int array[5][4];
int sum = 0;
cout<<"Enter grades for 4 exams for the 5 students \n";
for(int i=0;i<5;i++)
{
for(int b=1;b<=4;b++)
{
cout <<setw(8)<< "enter student "<< i << "'s grade for exam " << b << '\n';
cin >> array[i][b];
}
}
cout <<"ID"<<setw(11)<<"score 1"<<setw(11)<<"score 2"<<setw(11)<<"score 3"<<setw(11)<<"score 4"<<setw(11)<<"total"<<setw(11)<<"letter"<<endl;
cout <<"--------------------------------------------------------------------------------------------------------------\n";
for(int i=0;i<5;i++)
{
cout << i<< " ";
for(int b=1;b<=4;b++)
{
sum = sum + array[b];
cout <<setw(10)<<array[i][b]<<sum;
}
cout <<'\n';
}
cout <<"--------------------------------------------------------------------------------------------------------------\n";
return 0;
}
To be more specific around line 28
for(int i=0;i<5;i++)
{
cout << i<< " ";
for(int b=1;b<=4;b++)
{
sum = sum + array[b];
cout <<setw(10)<<array[i][b]<<sum;
}
cout <<'\n';
Arrays indexes start at 0, not 1. You are correctly looping through your array's 1st dimension, but not its 2nd dimension. You need to change the inner for loops from for(int b=1;b<=4;b++) to for(int b=0;b<4;b++)
Also, to handle the total column, you simply need to reset sum to 0 on each iteration of the 1st dimension, and then print the result after the iteration of the 2nd dimension.
Try this:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int array[5][4];
cout << "Enter grades for 4 exams for the 5 students \n";
for(int i = 0; i < 5; i++)
{
for(int b = 0; b < 4; b++)
{
cout << "Enter student " << i+1 << "'s grade for exam " << b+1 << '\n';
cin >> array[i][b];
}
}
cout << "ID" << setw(11) << "score 1" << setw(11) << "score 2" << setw(11) << "score 3" << setw(11) << "score 4" << setw(11) << "total" << setw(11) << "letter" << endl;
cout << "--------------------------------------------------------------------------------------------------------------\n";
for(int i = 0; i < 5; i++)
{
cout << setw(2) << left << i+1 << right;
int sum = 0;
for(int b = 0; b < 4; b++)
{
sum = sum + array[i][b];
cout << setw(11) << array[i][b];
}
cout << setw(11) << sum << '\n';
}
cout << "--------------------------------------------------------------------------------------------------------------\n";
return 0;
}
Online Demo
So I'm trying to write a program that will read in ten whole numbers and outputs the sum of all numbers greater than zero, the sum of all numbers less than zero,
and the sum of all numbers, whether positive, negative, or zero.
Currently only the total sum and negative sum is adding correctly. My positive sum is always 1 or 0. Any tips to get me in the right direction would help.
Current Code:
#include <iostream>
using namespace std;
int main()
{
int N = 0;
int sum = 0;
int positiveSum = 0;
int negativeSum = 0;
cout << "Number 1" << endl;
cin >> N;
cout << "Number 2" << endl;
cin >> N;
cout << "Number 3" << endl;
cin >> N;
cout << "Number 4" << endl;
cin >> N;
cout << "Number 5" << endl;
cin >> N;
cout << "Number 6" << endl;
cin >> N;
cout << "Number 7" << endl;
cin >> N;
cout << "Number 8" << endl;
cin >> N;
cout << "Number 9" << endl;
cin >> N;
cout << "Number 10" << endl;
cin >> N;
for(int i=0;i<10;i++)
{
if (N >= 0 )
{
positiveSum += N;
}
else
{
negativeSum += N;
}
}
sum = positiveSum + negativeSum;
cout << "The positive sum is= " << positiveSum << endl;
cout << "the negative sum is= " << negativeSum << endl;
cout << "The total sum is= " << sum << endl;
return 0;
}
After entering all Ns and when entring to the loop, you have only the last N. because after assigning the first number to N you don't process the value but you assigning the next value to N again after asking the user to enter it and so on. Use something like the following
#include <iostream>
int main()
{
int N = 0;
int sum = 0;
int positiveSum = 0;
int negativeSum = 0;
for(int i=0;i<10;i++)
{
std::cout << "Number "<<i + 1<< std::endl;
std::cin >> N;
if (N >= 0 )
{
positiveSum += N;
}
else
{
negativeSum += N;
}
}
sum = positiveSum + negativeSum;
std::cout << "The positive sum is= " << positiveSum << std::endl;
std::cout << "the negative sum is= " << negativeSum << std::endl;
std::cout << "The total sum is= " << sum << std::endl;
return 0;
}
And see Why is "using namespace std;" considered bad practice?
How do I find the smallest value in an array? I think I'm doing it right, but it outputs zero when I run the program.
I did it the same way in another program and it worked. When this runs, the highest element displays, but the lowest displays as zero.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int ARRAY_SIZE = 12;
double rainfall[ARRAY_SIZE];
double total_year, monthly_average;
double highest = rainfall[0];
double lowest = rainfall[0];
cout << " Input rainfall for each month: \n" ;
for(int index = 0; index < ARRAY_SIZE; index++)
{
cout << " Month " << index+1 << ": " ;
cin >> rainfall[index];
total_year += rainfall[index];
if(rainfall[index] < 0)
{
cout << " Rainfall must equal to 0 or higher: " ;
cin >> rainfall[index];
}
}
for(int x = 0; x < ARRAY_SIZE; x++)
{
if(highest < rainfall[x])
{
highest = rainfall[x];
}
if(lowest > rainfall[x])
{
lowest = rainfall[x];
}
}
cout << fixed << setprecision(2) << endl;
cout << " There was " << total_year << " inches" ;
cout << " of rainfall this year. \n" ;
cout << " The monthtly average was " << total_year / 12 ;
cout << " inches of rainfall.\n";
cout << " The highest rainfall was " << highest << " inches" << endl;
cout << " The lowest rainfall was " << lowest << " inches" << endl;
return 0;
}
Try to declare variables where they are used. Otherwise the code will be less readable.
The array rainfall is not yet initialized
double rainfall[ARRAY_SIZE];
//...
double highest = rainfall[0];
double lowest = rainfall[0];
So using its elements with indeterminate values for the variables highest and lowest does not make sense.
Declare and initialize the variables just before the loop where they are calculated.
double highest = rainfall[0];
double lowest = rainfall[0];
for(int x = 0; x < ARRAY_SIZE; x++)
{
if(highest < rainfall[x])
{
highest = rainfall[x];
}
if(lowest > rainfall[x])
{
lowest = rainfall[x];
}
}
In this loop
for(int index = 0; index < ARRAY_SIZE; index++)
{
cout << " Month " << index+1 << ": " ;
cin >> rainfall[index];
total_year += rainfall[index];
if(rainfall[index] < 0)
{
cout << " Rainfall must equal to 0 or higher: " ;
cin >> rainfall[index];
}
}
move the statement
total_year += rainfall[index];
after the if statement.
for(int index = 0; index < ARRAY_SIZE; index++)
{
cout << " Month " << index+1 << ": " ;
cin >> rainfall[index];
if(rainfall[index] < 0)
{
cout << " Rainfall must equal to 0 or higher: " ;
cin >> rainfall[index];
}
total_year += rainfall[index];
}
I would substitute the if statement for a while statement like
while (rainfall[index] < 0)
{
cout << " Rainfall must equal to 0 or higher: " ;
cin >> rainfall[index];
}
but before using the variable total_year you have to initialize it
double total_year = 0.0;
The variable monthly_average is not used in the code. So its declaration can be removed.
Take into account that there are the following algorithms in C++ std::min_element, std::max_element, std::minmax_element that can be used to find minimum and maximum alements in an array or other container.
when I run this program is a "Run-Time Check Failure #2 stack around the variable 'numGrades' was corrupted" appears. Also the lowest grade doesn't output the correct answer. Any help would be greatly appreciated!
#include <iostream> // cin, cout
using namespace std;
const int TEN_GRADES = 10; // pre-defined number of grades
int main()
{
int numGrades[10];
double avg, highest = numGrades[0], lowest = numGrades[0], less, greater, grades;
double sum = 0;
// greeting message
cout << "---------------------------------" << endl
<< " Sandro's Statistics Generator " << endl
<< "---------------------------------" << endl << endl;
// requesting number of grades
cout << "Hello Professor, how many grades do I need to analyse this time? ";
cin >> numGrades[10];
if (numGrades[1] == 0)
{
cout << "\nGuess you changed your mind!!!" << endl
<< "Ending program now..." << endl << endl;
return 0;
}
// if user doesn't enter 0 user is ready to begin
cout << "Okay, I am ready. Start..." << endl;
for (int count = 0; count < numGrades[10]; count++)
{
cin >> numGrades[count];
sum += numGrades[10];
}
// to get the average
avg = sum / TEN_GRADES;
// to get the highest and lowest mark
for (int count = 0; count < TEN_GRADES; count++)
{
if (numGrades[count] > highest)
highest = numGrades[count];
}
for (int count = 0; count < TEN_GRADES; count++)
{
if (numGrades[count] < lowest)
lowest = numGrades[count];
}
// output requested statistics
cout << "Here are the requested stats for the " << numGrades << " grades." << endl
<< "The class average is " << avg << endl
<< "The highest grade is " << highest << endl
<< "The lowest grade is " << lowest << endl;
return 0;
}
Oh god, I don't even do c++ but I think one of my eyes bled a little.
Please review (or tell whoever coded this to review) how to create and assign values to them.
Then review simple data structures (like arrays) and loops.
One good way to start is to analyze the following WORKING code of your program:
Please mark as correct if it helps, and if you have any questions... Cheers!
#include <iostream>
#include <string>
using namespace std;
const int TEN_GRADES = 10; // pre-defined number of grades
int main()
{
int numGrades[10];
double avg, highest = 0, lowest = 0, less, greater, grades;
double sum = 0;
// greeting message
cout << "---------------------------------" << endl
<< " Newbie Statistics Generator " << endl
<< "---------------------------------" << endl << endl;
// requesting number of grades
cout << "Hello Professor, please enter 10 grades: "<<endl;
//THIS PART: loops ten times to input the grades
for (int count = 0; count < TEN_GRADES; count ++)
{
cout << "Grade number "<<count<<":";
cin >> numGrades[count];
}
//I get what you want to do here, but consider adding another exit condition here, what if the second grade is really 0 ?
if (numGrades[1] == 0)
{
cout << "\nGuess you changed your mind!!!" << endl
<< "Ending program now..." << endl << endl;
return 0;
}
// if user doesn't enter 0 user is ready to begin
cout << "Okay, I am ready. Start..." << endl;
for (int count = 0; count < TEN_GRADES; count++)
{
sum += numGrades[count];
}
// to get the average
avg = sum / TEN_GRADES;
// to get the highest and lowest mark
for (int count = 0; count < TEN_GRADES; count++)
{
if (numGrades[count] > highest)
highest = numGrades[count];
}
for (int count = 0; count < TEN_GRADES; count++)
{
if (numGrades[count] < lowest)
lowest = numGrades[count];
}
// output requested statistics
cout << "Here are the requested stats for the " << TEN_GRADES << " grades." << endl
<< "The class average is " << avg << endl
<< "The highest grade is " << highest << endl
<< "The lowest grade is " << lowest << endl;
return 0;
}
This is a selection sort that I'm trying and I don't understand why it isn't working. My understanding is that selection sort scans a vector for the smallest value which when it finds it moves it to the beginning of the vector. It preforms another scan this time ignoring the first element and doing it all over again until n - 1 times where n is the length of the vector.
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <map>
using namespace std;
int main()
{
vector <string> vName, vID, vClass;
string sName, minValue, sID, sClass, sSearch, sQuestion, ssSearch, sSearchN, sSearchI;
int iSize, iStudent;
// Display initial vector size
iSize = vName.size();
cout << "Student list starts with the size:" << iSize << endl;
// Get size of list from user
cout << "How many students would you like to add?" << endl;
cin >> iStudent;
cin.ignore();
// Get names, ids, and classes
for (int i = 0; i < iStudent; i++)
{
cout << "Student" << i + 1 << ":\n";
cout << "Please enter the student name: ";
getline(cin, sName);
vName.push_back(sName);
cout << "Enter ID number ";
getline(cin, sID);
vID.push_back(sID);
cout << "Enter class name ";
getline(cin, sClass);
vClass.push_back(sClass);
}
// Display header
cout << "The list of students has the size of: " << iStudent << endl;
cout << "The Student List" << endl;
cout << "\n";
cout << "Name:" << setw(30) << "ID:" << setw(38) << "Enrolled Class : " << endl;
cout << "--------------------------------------------------------------------------";
cout << "\n";
// for loop for displying list
for (int x = 0; x < vName.size() && vID.size() && vClass.size(); x++)
{
cout << vName[x] << "\t \t \t" << vID[x] << "\t \t \t" << vClass[x] << endl;
}
// Sorting function and for loop to display sorted names
cout << "\n";
cout << "The Student List after Sorting:" << endl;
cout << "\n";
//*************************************
int startScan, minIndex;
for (startScan = 0; startScan < vName.size() - 1; startScan++)
{
minIndex = startScan;
minValue = vName[startScan];
for (int index = startScan + 1; index < vName.size(); index++)
{
if (vName[index] < minValue)
{
minValue = vName[index];
minIndex = index;
}
}
vName[minIndex] = vName[startScan];
vName[startScan] = minValue;
vID[minIndex] = vID[startScan];
vID[startScan] = minValue;
vClass[minIndex] = vClass[startScan];
vClass[startScan] = minValue;
}
//******************
//sort(vName.begin(), vName.end());
//for (int y = 0; y < vName.size(); y++)
//{
// cout << vName[y] << endl;
//}
cout << "\n";
// Search function uses a do while loop that loops so long as the user inputs a "y" or "Y"
do
{
int iPick;
cout << "Search Menu:" << endl;
cout << "1. By Name\n";
cout << "2. By ID\n \n";
cin >> iPick;
if (iPick == 1)
{
cout << "Please Enter a name to be searched:" << endl;
getline(cin >> ws, sSearchN);
if (binary_search(vName.begin(), vName.end(), sSearchN))
{
cout << sSearchN << " was found." << endl << endl;
}
else
{
cout << sSearchN << " was not found." << endl << endl;
}
cout << "Would you like to search another name?" << endl << endl;
cout << vName[0];
cout << "Please enter Y for Yes and N for No:" << endl << endl;
getline(cin >> ws, sQuestion);
}
else
{
cout << "Please Enter an ID to be searched:" << endl;
getline(cin >> ws, sSearchI);
if (binary_search(vID.begin(), vID.end(), sSearchI))
{
cout << sSearchI << " " << "was found" << endl << endl;
cout << "Please enter Y for Yes and N for No:" << endl << endl;
getline(cin >> ws, sQuestion);
}
else
{
cout << sSearchI << " " << "was not found." << endl << endl;
cout << "Please enter Y for Yes and N for No:" << endl << endl;
getline(cin >> ws, sQuestion);
}
}
} while (sQuestion == "Y" || sQuestion == "y");
cout << "Thank you for using this program!" << endl;
return 0;
}
EDIT: posted whole program
So the issue is it doesn't sort at all, just gives back vName unsorted.
Okay upon more further study of the code I think I've figured it out.
So looking at the part of the code that is sorting, the way that it works is actually pretty simple. startScan is an int increments until it becomes equal to the size of the vector in the for loop. In this case it is vName. minIndex will hold the smallest index number which at the time of initialization will be equal to the starScan. Finally minValue is a string (in this case because we have a vector of strings) that acts as a temporary container for the element at vName[scanStart]. In the second for loop index will be incremented through the vector and will test if the element vName[index] is smaller than the temproray container i talked about earlier. If it is than the new temp will be vName[index] after that is done it will exit the inner loop and update and move on to the next smallest value in vName. Understanding this it's easy to make it so that the sorting happens with 2 other vectors. All one has to do is create containers for those vectors like the ones i have here minValueA and minValueB. They will simply be changing as vName changes. That way everything stays in the order in which vName is ordered. I hope that helps someone!
int startScan, minIndex;
string minValue, minValueA, minValueB;
for (startScan = 0; startScan < vName.size() - 1; startScan++)
{
minIndex = startScan;
minValue = vName[startScan];
minValueA = vID[startScan];
minValueB = vClass[startScan];
for (int index = startScan + 1; index < vName.size(); index++)
{
if (vName[index] < minValue)
{
minValue = vName[index];
minValueA = vID[index];
minValueB = vClass[index];
minIndex = index;
}
}
vName[minIndex] = vName[startScan];
vName[startScan] = minValue; //values for vName are being added to the other ones.
vID[minIndex] = vID[startScan];
vID[startScan] = minValueA;
vClass[minIndex] = vClass[startScan];
vClass[startScan] = minValueB;
}
//******************
sort(vName.begin(), vName.end());
for (int y = 0; y < vName.size(); y++)
{
cout << vName[y] << "\t \t \t " << vID[y] << "\t \t \t " << vClass[y] << endl;
}