Trying to write a simple program that takes six test grades, input by the user, drops the lowest, and then displays average of the remaining five. The program compiles fine, but outputs the wrong answer. Any pointers to where the logic error is occurring would be helpful. I am using these values as sample grades:
85.2
92.3
78.0
51.5
91.6
87.0
and the value returned should be 86.82, but the program is instead returning 80.08.
#include <iostream>
using namespace std;
const int SIZE = 6;
double grades[SIZE];
double low(double[]);
int main()
{
double preavg = 0;
double avg = 0;
cout << "This program will take an input of six test grades\n"
<< "and determine the average of the highest five grades.\n\n"
<< "Please input the first test grade\n";
cin >> grades[0];
cout << "Please input the second test grade\n";
cin >> grades[1];
cout << "Please input the third test grade\n";
cin >> grades[2];
cout << "Please input the fourth test grade\n";
cin >> grades[3];
cout << "Please input the fifth test grade\n";
cin >> grades[4];
cout << "Please input the sixth test grade\n";
cin >> grades[5];
for (int i = 0; i < SIZE; i++)
{
preavg += grades[i];
}
avg = (preavg - low(grades)) / (SIZE -1);
cout << "Your average grade is " << avg << "\n";
system("PAUSE");
return 0;
}
double low(double grades[SIZE])
{
double lowest;
lowest = 100;
for (int i = 0; i < SIZE; i++)
{
if (grades[i] < lowest)
lowest = grades[i];
}
return lowest;
}
Your issue here is possibly coming from the fact you're doing division in double precision using a const int. If you cast SIZE as a double and write the rest of your double numbers with a .0 on the end, it works fine:
#include <iostream>
using namespace std;
const int SIZE = 6;
double grades[SIZE];
double low(double[]);
int main()
{
double preavg = 0.0;
double avg = 0.0;
cout << "This program will take an input of six test grades\n"
<< "and determine the average of the highest five grades.\n\n"
<< "Please input the first test grade\n";
cin >> grades[0];
cout << "Please input the second test grade\n";
cin >> grades[1];
cout << "Please input the third test grade\n";
cin >> grades[2];
cout << "Please input the fourth test grade\n";
cin >> grades[3];
cout << "Please input the fifth test grade\n";
cin >> grades[4];
cout << "Please input the sixth test grade\n";
cin >> grades[5];
for (int i = 0; i < SIZE; i++)
{
preavg += grades[i];
}
avg = (preavg - low(grades)) / ((double)SIZE - 1.0);
cout << "Your average grade is " << avg << "\n";
system("PAUSE");
return 0;
}
double low(double grades[SIZE])
{
double lowest;
lowest = 100.0;
for (int i = 0; i < SIZE; i++)
{
if (grades[i] < lowest)
lowest = grades[i];
}
return lowest;
}
Related
I have the following code
for (int i = 0; i < courses; i++)
{
cout << "Please Enter Letter Grade: ";
cin >> grade1;
cout << "Please Enter Course Weighting: ";
cin >> weight1;
}
Now, lets say the loop runs 3 times and the values entered by the user for grade1 and weight1 are different each time. I want to store these different values so I can do some calculations with them. How would I proceed to do so?
Here is how use an array:
int grade[courses]; // this is an array with size of courses
double weight[courses];
for (int i = 0; i < courses; i++) {
cout << "Please Enter Letter Grade: ";
cin >> grade[i];
cout << "Please Enter Course Weighting: ";
cin >> weight[i];
}
Array is collection of data of the same type stored sequentially in computer memory. Syntax for array is as follow:
<type> <name>[<size>];
for example
int numberOfStudents[100];
is int array with maximum of 100 elements.
Hope This Helps
group grade and weight into a struct and store them in a vector.
code: (doesnt handle all potential errors)
#include <iostream>
#include <vector>
struct grade_weight
{
int grade;
int weight;
};
int main()
{
int courses = 5;
std::vector<grade_weight> result;
// potential optimization if you want
//result.reserve(courses);
for (int i = 0; i < courses; i++)
{
int grade, weight;
std::cout << "Please Enter Letter Grade: ";
std::cin >> grade;
std::cout << "Please Enter Course Weighting: ";
std::cin >> weight;
result.push_back({grade, weight});
}
std::cout << "you input\n";
for(auto& gw: result)
{
std::cout << "grade: " << gw.grade << ", weight: " << gw.weight << '\n';
}
}
Creating a Script to Calculate multiple students and multiple graded assignments that are weighted different amounts.
//
// main.cpp
// PWGHelper
//
// Created by Lyle Baer on 1/18/16.
// Copyright © 2016 Lyle Baer. All rights reserved.
//
//
#include <iostream>
#include <vector>
using namespace std;
// Main Script
int main()
{
// Vector to store Calculated Grades
vector <int> calculatedGrades;
cout << "Enter Number of Students: \n\n";
int(nStudent);
cin >> nStudent;
// Loop
if (nStudent < 1)
{
const int MAX = 50;
float grades[MAX];
float weights[MAX];
// Greeting
cout << "Hello, its me\n\n";
cout << "Your Personal Weighted Grade Helper\n\n";
cout << "Call me PWG Helper for short.\n\n";
// Input # of exercises
cout << "Enter number of exercises: ";
int num;
cin >> num;
// Input Weights
int correct = 0;
while (correct == 0)
{
for (int i = 0; i < num; i++)
{
cout << "\nWhat is the weight in percent? ";
cin >> weights[i];
weights[i] = weights[i] / (float)100.0;
}
correct = 1;
}
// Input Grades
for (int i = 0; i < num; i++)
{
cout << "\nWhat is the Grade? ";
cin >> grades[i];
}
// Values
cout << "Printed Values\n";
for (int i = 0; i < num; i++)
{
cout << grades[i] << "\n";
cout << weights[i] << "\n";
}
// Find Average
float sum = 0;
for (int i = 0; i < num; i++)
{
sum = sum + grades[i] * weights[i];
}
// Total Sum
cout << "This student has a score of: " << sum;
cin >> sum;
calculatedGrades.push_back(sum);
// Keep console open
cin.get();
cin.get();
}
else
{
// Variables
const int MAX = 50;
float grades[MAX];
float weights[MAX];
// Greeting
cout << "Hello, its me\n\n";
cout << "Your Personal Weighted Grade Helper\n\n";
cout << "Call me PWG Helper for short.\n\n";
// Input # of exercises
cout << "Enter number of exercises: ";
int num;
cin >> num;
// Input Weights
int correct = 0;
while (correct == 0)
{
for (int i = 0; i < num; i++)
{
cout << "\nWhat is the weight in percent? ";
cin >> weights[i];
weights[i] = weights[i] / (float)100.0;
}
correct = 1;
}
// Input Grades
for (int i = 0; i < num; i++)
{
cout << "\nWhat is the Grade? ";
cin >> grades[i];
}
// Values
cout << "Printed Values\n";
for (int i = 0; i < num; i++)
{
cout << grades[i] << "\n";
cout << weights[i] << "\n";
}
// Find Average
float sum = 0;
for (int i = 0; i < num; i++)
{
sum = sum + grades[i] * weights[i];
}
//Total Sum
cout << "This student has a score of: " << sum;
cin >> sum;
calculatedGrades.push_back(sum);
// Keep console open
cin.get();
cin.get();
}
return 0;
}
Example output of this code:
Enter Number of Students: 3
Hello, its me
Your Personal Weighted Grade Helper
Call me PWG Helper for short.
Enter number of exercises: 2
What is the weight in percent? 10
What is the weight in percent? 10
What is the Grade? 85
What is the Grade? 97
Printed Values
85
0.1
97
0.1
This student has a score of: 18.2
If I input this code:
#include<iostream>
using namespace std;
int main ()
{
int input, qty, min, max;
bool validity = 1;
cout << "How many integers would you like to enter? \n";
cin >> qty;
if (qty <= 0)
cout << "Please enter a positive number\n";
else
{
cout << "Please enter " << qty << " integers:" << endl;
for (int x=0; x < qty; x++)
{
cin >> input;
if (input > max)
max = input;
if (input < min)
min = input;
}
}
if (validity)
{
cout << "Minimum: " << min << endl;
cout << "Maximum: " << max << endl;
}
return 0;
}
It works as expected.
But if I have this:
#include<iostream>
using namespace std;
int main ()
{
int input, qty, min, max;
bool validity = 1;
cout << "How many integers would you like to enter? \n";
cin >> qty;
if (qty <= 0)
cout << "Please enter a positive number\n";
else
{
cout << "Please enter " << qty << " integers:" << endl;
for (int x=0; x < qty; x++)
{
cin >> input;
if (input > max)
max = input;
if (input < min)
min = input;
}
}
if (max > 2147483646)
{
cout << "Please enter a valid value for integers." << endl;
validity = 0;
}
if (min < -2147483647)
{
cout << "Please enter a valid value for integers." << endl;
validity = 0;
}
if (validity)
{
cout << "Minimum: " << min << endl;
cout << "Maximum: " << max << endl;
}
return 0;
}
It gives me erroneous values.
What am I doing wrong? Any help is greatly appreciated. (I'm a noob btw). Adding a little text here so that I can post this question.............................
max and min are uninitialized. In C++, this means the values can be anything at all, unless in say Java where primitives are auto-initialized to 0.
One way to fix is to set a first flag, and set max and min the the first value entered.
bool first = true;
for (int x=0; x < qty; x++)
{
cin >> input;
if( first )
{
max = input;
min = input;
first = false;
}
if (input > max)
max = input;
if (input < min)
min = input;
}
Here's what I mean by uninitialized. When starting up, min and max can be anything. Anything at all. Try it by printing out the value of min and max before your loop. It should (could) be different every time you run the program. Basically the value depends on what data was in that memory location the last time it was used.
So the if( input > max) is checking to see if input is greater than some random number between -2billion and 2 billion. (not useful). The first flag I put in there initializes min/max in the first iteration of the for loop, or the first value entered by the user, which is guaranteed to be both the min and the max of the entered value since it's the only entered value.
Hello i am working on a program to store a users input of prices into an array. then have a module accept the price array and return tax. here is what i have so far. my module keeps producing LNK2019.
#include <iostream>
using namespace std;
double PrArray[20] = {}, total_price, tax_rate, price, tax;
int i;
double TAXOWED();
void main()
{
cout << "input the sales tax rate as a decimal ";
cin >> tax_rate;
cout << "\nplease enter a price or 0 to quit ";
i = 0;
cin >> price;
PrArray[i] = price;
while (price > 0 && i < 19)
{
i++;
cout << "please enter a price or 0 to quit ";
cin >> price;
PrArray[i] = price;
}
for (i = 0; i < 19; i++)
{
cout << "\n";
cout << PrArray[i];
}
TAXOWED();
system("PAUSE");
return;
}
double TAXOWED(double PrArray[], int i)
{
double total_tax;
for (i = 0; i < 19; i++)
{
tax = tax_rate * PrArray[i];
total_tax = tax;
}
return(total_tax);
}
Your function declaration and definition don't match
// Declaration
double TAXOWED();
// Definition
double TAXOWED(double PrArray[], int i);
So essentially your are declaring a function and never defining it, then defining a separate function below.
Relaying on your comment, the code should looks like this:
#include <iostream>
using namespace std;
double TAXOWED(double price, double tax_rate)
{
return tax_rate * price;
}
int main()
{
double PrArray[20], total_tax[20], tax_rate, price;
cout << "input the sales tax rate as a decimal ";
cin >> tax_rate;
cout << "\nplease enter a price or 0 to quit ";
cin >> price;
if ( price == 0 )
return -1;
int i = 0;
PrArray[i] = price;
for (i = 1; i < 20; i++)
{
cout << "please enter a price or 0 to quit " ;
cin >> price;
PrArray[i] = price;
}
for (int i = 0 ; i < 20; i++)
total_tax[i] = TAXOWED(PrArray[i], tax_rate);
for (int i = 0; i < 20; i++)
cout << "Tax for price #" << i << " : " << total_tax[i] << endl;
system("PAUSE");
return 0;
}
This code will allow you to set tax_rate, each price value and output tax_rate for each price.
Ok this is what i have now. i need the chart at the end to have all the total prices added together
#include <iostream>
using namespace std;
// declerations
double PrArray[20], total_tax[20], tax_rate, price, total_price;
int i;
double TAXOWED(double price, double tax_rate)
{
return tax_rate * price;
}
void main()
{
cout << "input the sales tax rate as a decimal ";
cin >> tax_rate;
cout << "\nplease enter a price or 0 to quit ";
i = 0;
cin >> price;
PrArray[i] = price;
while (price > 0 && i < 19)
{
i++;
cout << "please enter a price or 0 to quit ";
cin >> price;
PrArray[i] = price;
}
for (int i = 0; i < 20; i++)
{
total_tax[i] = TAXOWED(PrArray[i], tax_rate);
}
system("CLS");
cout << " Prices";
cout << "\ntaxrate % " << tax_rate;
cout << "\nprices tax Total price ";
cout << "\n__________________________________________________";
for (int i = 0; i < 20; i++)
{
cout << "\n" << PrArray[i] << " " << total_tax[i] << " " << PrArray[i] + total_tax[i];
}
for (int)
system("PAUSE");
return;
}
so here is my code (stripped of the headers because that is irrerevelant.)
int main() {
float program = 0;
float scores = 0;
float test = 0;
float testScores = 0;
float e = 1;
float exam = 0;
float programAverage = 0;
cout << "Enter the number of assignments that were graded: ";
cin >> program;
for (int i = 1; i <= program; i++){
cout << "Enter the score for assignment # " << i <<": "; cin >> scores;
}
cout << "Enter the number of test: ";
cin >> test;
for (int e = 1; e <= test; e++){
cout << "Enter the score for test # " << e << ": "; cin >> testScores;
}
cout << "Enter the final exam score: ";
cin >> exam;
programAverage = (scores/program);
cout << "Program Average: " << programAverage << endl;
}
the last part I am having problems because whenever I compile my program the compiler just remembers the last number the user entered and does not average it. How can i get it to add all the user input numbers together and then average?
int main() {
float program = 0;
float scores = 0;
float test = 0;
float testScores = 0;
float e = 1;
float exam = 0;
float programAverage = 0;
float scoresSum = 0; // variable that adds up all the input scores
cout << "Enter the number of assignments that were graded: ";
cin >> program;
for (int i = 1; i <= program; i++){
cout << "Enter the score for assignment # " << i <<": "; cin >> scores;
scoresSum += scores; // adds up all the scores
}
cout << "Enter the number of test: ";
cin >> test;
for (int e = 1; e <= test; e++){
cout << "Enter the score for test # " << e << ": "; cin >> testScores;
}
cout << "Enter the final exam score: ";
cin >> exam;
programAverage = (scoresSum/program); // divide the total score out of program number
cout << "Program Average: " << programAverage << endl;
}
So the problem was that you didn't add up the input scores.
The variable "scores" only has the value of the last input score.
You have to set up a variable to sum up all the input score so far, such as scoresSum in the code.
And add up the score every time a score is submitted.
You can easily find the difference between your code and mine by looking at the line with comment.
float _sum=0;
for (int i = 1; i <= program; i++){
cout << "Enter the score for assignment # " << i <<": "; cin >> scores;
_sum+=i;
}
programAverage = (_sum/program);
cout << "Program Average: " << programAverage << endl;
Well yeah because of this loop, scores always has the last value entered:
for (int i = 1; i <= program; i++){
cout << "Enter the score for assignment # " << i <<": "; cin >> scores;
}
An average is defined as a sum divided by the number of instances. You're not summing, you just keep overwriting "scores" with the last read value when you do cin >> scores. So the problem can be restated as "How can I sum all the numbers the user entered in?" Computers do exactly what you tell them to, and you need to figure out how to exactly tell it to sum all the entered scores.
Well how would you do that in real life? You'd keep a running tally of all the scores, maybe by adding them with a calculator. You'd first initialize a count:
double sum = 0.0;
Then after the line for `cout << "Enter the score..." you add to sum:
sum = sum + scores;
Or C++ has handy shorthand notation
sum += scores