How to calculate de average from user input numbers [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
So basically, I'm new to programming and can't figure it out how to add certain numbers generated by the user. I want a program, capable of collecting for n days the temperature, and at the end, inform what the peak heat occurred, as well as the average of the temperatures collected.
#include <iostream>
using namespace std;
int main()
{
int days, day0 = 0, degrees, degrees_max = 0;
float degrees_average;
cout << "Days of the study" << endl;
cin >> days;
while (days > day0)
{
cout << "How many Degrees?" << endl;
cin >> degrees;
if (degrees < 999)
{
day0++;
}
if (degrees > degrees_max)
{
degrees_max = degrees;
}
}
degrees_average = degrees / days;
cout << "The max temperature was: " << degrees_max << "C"
<< " And the average was : " << degrees_average << endl;
}
SO the only thing left is how to calculate the average.
All the help is appreciated! Thanks

#include <iostream>
using namespace std;
int main()
{
int days, max=-1;
double current, total = 0;
cin >> days;
for (int i=0; i<days; i++){
cin >> current;
if (current > max) max = current;
total = total + current;
}
cout << "max: " << max << endl;
cout << "average: " << total/days << endl;
return 0;
}

Related

I need to calculate each students average grade [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
/*This code creates two text files : Mokiniai.txt and Vidurkiai.txt
In Mokiniai.txt there are stored each students grades, in Vidurkiai there
should be calculated each students average grade*/
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int n, k, isvisopazymiu, i, pazymiai;
double vidurkis; //average grade
ofstream myfile;
myfile.open("Mokiniai.txt");
cout << "parasykite kiek mokiniu yra" << endl; cin >> n; //how many students
myfile << n << endl;
cout << "parasykite kiek yra mokiniu pazymiu"; cin >> isvisopazymiu; // how many grades one student has
for (k = 1; k <= n; k++) {
for (i = 1; i <= isvisopazymiu; i++) {
cout << "Parasykite kokie yra mokiniu pazymiai "; cin >> pazymiai; // what are students grades
myfile << " " << pazymiai;
}
myfile << endl;
}
myfile.close();
myfile.open("Vidurkiai.txt"); //trying to calculate average students grades on different file
for (k = 1; k <= n; k++) {
vidurkis = pazymiai / isvisopazymiu; //calculating students grades
myfile << k << " " << vidurkis << endl;
}
myfile.close();
return 0;
}
My problem is that: There is something wrong in vidurkiai.txt file, but I don't know what is wrong.
For example: first students grades are : 7 8 9 7 8, and average grade should be 7,8, but after coding it, in vidurkiai.txt file it shows, that average grade is 1.
The reason why it does not show the grades as expected is because you are not adding up the values.While taking the average over all the grades variable
'pazymiai' stores just the last entered variable. In order to complete the job you can use an 'array' to stores sum of grades in each array element .
cin>>pazymiai;
a[k]+=pazymiai;
where as while calculating grades use
vidurkis = a[k] / isvisopazymiu;

_sleep() function not working [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I get the error:
'_sleep': This function or variable has been superceded by newer
library or operating system functionality. Consider using Sleep
instead. See online help for details.
Can you guys and/or girls tell me what library is causing this error?
Code:
// A program to keep track of points and time and to give a random letter for the game scattergories
#include<iostream>
#include<ctime>
#include<string>
using std::cout;
using std::cin;
using std::string;
using std::getline;
void ltr() //gives a random letter
{
srand(time(NULL)); //gives a differant pattern every time
char letter;
letter = rand() % 27 + 64; //assigns a random letter in ascii code to a char (resulting in a random letter)
cout << "The letter is " << letter << "\n";
}
void timer()
{
cout << "You got 1.5 minutes to finish\n";
for (int i = 90; i > 0; i--)
{
if (i % 5 == 0)
cout << i << "\n";
_sleep(1000);
}
cout << "DING DONG!!! DING DONG!!! Time's up!!!\n";
}
void table()
{
int plr, ctr;
string lst[5][20]; //first dimantion: how many players. second dimantion: how many catagories, third dimantion(if added) will be the round
cin >> plr >> ctr; //parameters for later
cin.ignore(); //To avoid the "getline" reading the last input
for (int x = 0; x<plr; x++) //the player changes only after the previus player finishes
{
timer();
for (int i = 0; i<ctr; i++) //changing catagory
{
getline(cin, lst[x][i]);
}
system("cls");
cout << "Next player\n";
}
for (int x = 0; x<plr; x++) //this part (the whole "for" loop) is for confirming
{
cout << "Player number " << x + 1 << ": ";
for (int i = 0; i<ctr; i++)
{
cout << lst[x][i] << " ";
}
cout << "\n";
}
_sleep(5000);
}
int points() //points per round
{
int a, b, c, sum;
cout << "How many sections only you got?\n"; //worth 15 points
cin >> a;
cout << "How many words only you got?\n"; //worth 10 points
cin >> b;
cout << "How many words you and another person got?\n"; //worth 5 points
cin >> c;
sum = a * 15 + b * 10 + c * 5;
return sum; //Note: It doesn't matter how many sections there are.
}
int act()
{
int Points;
ltr();
table();
Points = points();
cout << "You have earned " << Points << " this round\n\n";
return Points;
}
int main()
{
int Points;
cout << "Starting in five seconds\n";
_sleep(5000);
Points = act();
for (;;) //inf loop
{
int ph;
cout << "Press 1 to continue or anything else to stop\n";
cin >> ph;
if (ph == 1)
{
Points += act();
}
else
{
break;
}
}
cout << "You have earned a total of " << Points << " great job!";
_sleep(5000); //time to read the last text
return 0;
}
/* To do list:
*Convert to arduino
*Make timer work in background of of table
*Check if words in the table (for differant players) are the same and give points accordingly
*Check if words are actual words (connect an online dictonary?)
*Make interface? (if possible and I have time to learn how)
*Think of what to do with Hardwear
*Comment rest of the code
*/
For using Sleep() function you need
#include <windows.h>

How can I reset my program? (without using goto) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Basically I have finished programming a little times tables quiz for my little brother.I am very new to programming and I have no idea how to reset my program back to the start. Can anyone shed some light on this?
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
int sum ;
int question = 0;
int Uanswer ;
int score = 0;
int *score_pointer = &score;
//============================================================//
cout<<"= Hello Welcome to Your Times Tables ="<<endl;
cout<<"= Please Select A Table To Learn Below ="<<endl;
cout<<"========================================"<<endl;
cout<<"####################################"<<endl; /* THE MENU */
cout<<"####|1|2|3|4|5|6|7|8|9|10|11|12|####"<<endl;
cout<<"####################################"<<endl;
//============================================================//
cout<<">>:";
cin>> sum;
cout<<"You Selected "<< sum <<endl; /*User Input For Table */
cout<<"Time to Learn Brain Power Go!"<<endl;
//============================================================//
while (question <12){
question = question +1;
cout<< question;
cout<< "x";
cout<< sum ;
cout<< "=:";
cin>> Uanswer;
int Aanswer = (sum * question);
if (Aanswer == Uanswer){
cout<<"Correct: Well done Ben :)"<<endl;
*score_pointer = *score_pointer+1;
} else {
cout<<"Incorrect: Are you even trying? :("<<endl;
}
}
//====================================//
cout<<"Well done You scored " << score << " Out of 12"<<endl; /*Tally of total score */
//====================================//
return 0;
}
Use a do while loop to enclose your code and set the condition to what you want.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
int flag=1;
do
{
//your code that needs to be repeated
//when you need to exit set flag=0
} while(flag);
}
Here is your program in a while loop. I would also recommend not using endl and instead using \n so you dont have to flush the stream so much.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
bool done = false;
while (!done) {
int sum;
int question = 0;
int Uanswer;
int score = 0;
int *score_pointer = &score;
//============================================================//
cout << "= Hello Welcome to Your Times Tables =" << endl;
cout << "= Please Select A Table To Learn Below =" << endl;
cout << "========================================" << endl;
cout << "####################################" << endl; /* THE MENU */
cout << "####|1|2|3|4|5|6|7|8|9|10|11|12|####" << endl;
cout << "####################################" << endl;
//============================================================//
cout << ">>:";
cin >> sum;
cout << "You Selected " << sum << endl; /*User Input For Table */
cout << "Time to Learn Brain Power Go!" << endl;
//============================================================//
while (question < 12){
question = question + 1;
cout << question;
cout << "x";
cout << sum;
cout << "=:";
cin >> Uanswer;
int Aanswer = (sum * question);
if (Aanswer == Uanswer){
cout << "Correct: Well done Ben :)" << endl;
*score_pointer = *score_pointer + 1;
}
else {
cout << "Incorrect: Are you even trying? :(" << endl;
}
}
//====================================//
cout << "Well done You scored " << score << " Out of 12" << endl; /*Tally of total score */
//====================================//
cout << "\nWould you like to try again? (y/n)";
char answer;
cin >> answer;
tolower(answer);
if (answer == 'n') done = true;
}
return 0;
}

C++ while loop not running [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
my while loop wont run on the conditions I set it.
The purpose of the program is to determine the amount of years it will take a deposited amount to mature using the deposit amount, interest rate and target amount.
The program just stops after the target amount has been entered, unless I change the while statement from <= to >= in which case it runs the loop but returns the number of years set at round 100's or 1000 etc...
#include <iostream>
#include <iomanip>
#include <string>
#include <math.h>
using namespace std;
int main()
{
//declare the variables
double rate,
balance = 0;
int deposit,
target,
years = 0;
cout << "****Lets make you some money!****" << endl << endl;
//input from the user
cout << "What is your deposit amount?: " << endl;
cin >> deposit;
cout << "What is your interest rate?: " << endl;
cin >> rate;
cout << "What is you target savings amount?: " << endl;
cin >> target;
rate = rate / 100;
while (balance <= target); //when i change this to balance >= target the 'while' runs but just returns years divisible by 100
{
// calculation
balance += deposit * pow((1 + rate), years);
//balance = balance*(1 + rate) + deposit; // alternate calculation
//years++;
//users savings target
cout << "You will reach your target savings amount in: " << balance << " years." << endl << endl << " That's not that long now is it?" << endl;
}
return 0;
}
Thanks in advance!
The problem is an unfortunate suffix:
while (balance <= target);
// ^
That is equivalently:
while (balance <= target) {
;
}
{
// calculation, which always runs exactly once
// regardless of what balance/target are
}
Just drop the semicolon.

How do I get this code to work? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Struggling with this, can't seem to get it to work. Here is what I need to do:
1.Ask the user how many weeks were in the landscaping season this year.
2.Dynamically create an array of double floating point data that can be used to store the sales for each week of the season.
3.Using a for loop, have the user enter in the sales data for the season.
4.Pass the array (and its size) into a function that will display all the sales for the weeks in a nice table.
5.Call another function that will take in the array as an argument and will total the sales for the season (returning the total to the main function).
6.Have the program tell the user the total sales for the season and the average sales per week.
7.After this delete the allocated memory and set the pointer to 0.
Any assistance helps always I don't quite understand functions, or pointers. and I've read about both a lot!!
#include <iostream>
#include <iomanip>
using namespace std;
int Weeks = 0;
double total;
double sales;
void SalesTable(int);
double Total (int, double[]);
double average;
int main()
{
double Season[Weeks];
cout << "How many weeks were in the landscaping season this year: ";
cin >> Weeks;
for (int i = 1; i<Weeks+1; i++)
{
cout << "Enter sales data for week " << i <<": ";
cin >> sales;
}
Season[Weeks]=sales;
SalesTable(Weeks);
Total(Weeks, Season);
average = total/Weeks;
cout << "The total sales for the season are: "<<setprecision(2)<<showpoint<<fixed<<total<< endl;
cout << "The average weekly sales were: "<<setprecision(2)<<showpoint<<fixed<<average;
return 0;
}
void SalesTable(int Weeks)
{
for(int i = 1; i<Weeks+1; i++)
{
cout << "Sales for week "<< i <<":" << sales <<endl;
}
}
double Total(int Weeks, double Season[])
{
for(int i = 1; i<Weeks+1; i++)
{
total += Season[Weeks];
}
return total;
}
Ok , using pointer to double it will do what you need :
int Weeks = 0;
double total;
double sales;
void SalesTable(int, double*);
double Total (int, double*);
double average;
int main ()
{
double *Season;
cout << "How many weeks were in the landscaping season this year: ";
cin >> Weeks;
Season = new double [ Weeks ];
for (int i = 1; i<Weeks+1; i++)
{
cout << "Enter sales data for week " << i <<": ";
cin >> sales;
Season[i-1]=sales;
}
SalesTable(Weeks, Season);
Total(Weeks, Season);
average = total/(double)Weeks;
cout << "The total sales for the season are: "<<showpoint<<fixed<<total<<endl;
cout << "The average weekly sales were: "<<showpoint<<fixed<<average;
delete[] Season;
return 0;
}
void SalesTable(int Weeks,double *Season)
{
for(int i = 1; i<Weeks+1; i++)
{
cout << "Sales for week "<< i <<":" << Season[i-1]<<endl;
}
}
double Total(int Weeks, double *Season)
{
for(int i = 1; i<Weeks+1; i++)
{
total += Season[i-1];
}
return total;
}
Since I can't comment yet, I'll post a new answer:
The code provided by T-D still doesn't work because of this: the print out for each sales week is always the same because it isn't accessing the elements inside of the Seasons array.
void SalesTable(int Weeks)
{
for(int i = 1; i<Weeks+1; i++)
{
cout << "Sales for week "<< i <<":" << Season[i - 1] << endl;
}
}
Also, to dynamically create an array which allows the user to control the size of the array at runtime use the following code:
double *season = new double[Weeks];
And because you dynamically created the array, you have to manually release its resources:
delete[] season;