Question #30950

write a c program that totals and averages the temperature for the last 12 days.The calculation of the total and mean can only be done when user inputs a correct password.A password is stored as a number the user must correctly input the password in three attempts.If user does not input correct password in three tries ,the program ends ,if the user guesses the correct password then they can input the 12 day temperature.The program must define and make use of two functions ,log on for entering a correct password and calc for getting and calculating total and average

Expert's answer

// question.cpp : Defines the entry point for the console application.

//

#include <iostream>

using namespace std;

bool log (int password) // function log. returns true if user input the password correctly

{

int tmp;

int count = 0; // number of attempts

do

{

& cout << "Please, input the password: ";

& cin >> tmp;

& ++count;

}

while (tmp != password && count < 3 );

if (tmp == password)

& return true;

return false;

}

void calc (double& total, double& mean) // function calc. returns total and mean

{

const int kDays = 12; // count of days

double temp[kDays];

cout << "Enter the temperature for the last " << kDays << " days:\n";

total = 0;

for(int i = 0; i < kDays; ++i)

{

& cin >> temp[i]; // entering days

& total += temp[i]; // finding total

}

mean = total / kDays; // finding mean

}

void main()

{

const int password = 12345;

if (log(password))

{

& double total, mean;

& calc (total, mean);

& cout << "Total; " << total << endl;

& cout << "Mean: " << mean << endl;

& // writing total and mean to the screen could be done in function calc(double& total, double& mean)

& // for in please move 47 and 48 line of code int the end of function calc(double& total, double& mean)

& // delete 45 line of code rewrite 25 line as "void calc()" and 46 as calc()

}

}

//

#include <iostream>

using namespace std;

bool log (int password) // function log. returns true if user input the password correctly

{

int tmp;

int count = 0; // number of attempts

do

{

& cout << "Please, input the password: ";

& cin >> tmp;

& ++count;

}

while (tmp != password && count < 3 );

if (tmp == password)

& return true;

return false;

}

void calc (double& total, double& mean) // function calc. returns total and mean

{

const int kDays = 12; // count of days

double temp[kDays];

cout << "Enter the temperature for the last " << kDays << " days:\n";

total = 0;

for(int i = 0; i < kDays; ++i)

{

& cin >> temp[i]; // entering days

& total += temp[i]; // finding total

}

mean = total / kDays; // finding mean

}

void main()

{

const int password = 12345;

if (log(password))

{

& double total, mean;

& calc (total, mean);

& cout << "Total; " << total << endl;

& cout << "Mean: " << mean << endl;

& // writing total and mean to the screen could be done in function calc(double& total, double& mean)

& // for in please move 47 and 48 line of code int the end of function calc(double& total, double& mean)

& // delete 45 line of code rewrite 25 line as "void calc()" and 46 as calc()

}

}

## Comments

## Leave a comment