For my class, I am needing to write a program using arrays instead of individual variables that contain the score. Struggling with this chapter. I'll leave my code below. Any help would be greatly appreciated. Needing to find the sum between all of the scores entered, subtract the highest and lowest and then find the average.
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototypes
void getJudgeData(double, double);
double calcScore(double);
double findLowest(double);
double findHighest(double);
int main()
{
// Declare variables
const int SIZE = 7;
double scores[SIZE];
double sum;
for (int x = 0; x < SIZE; x++)
double getJudgeData(scores[x]);
return 0;
}
// Function getJudgeData
void getJudgeData(double &judgeScore, double scores[])
{
static int judge = 1;
cout << "Enter score " << judge << ": ";
cin >> judgeScore;
while (judgeScore < 0 || judgeScore > 10)
{
cout << "Invalid score, please enter a score between 0 and 10";
cout << "Enter score " << judge << ": ";
cin >> judgeScore;
}
judge++;
return;
}
// Function calcScore
double calcScore(double scores[])
{
double highest = findHighest(scores);
double lowest = findLowest(scores);
double result = 0;
for (int x = 0; x < 7; x++)
{
result += scores[x];
}
result - findHighest(scores) - findLowest(scores);
cout << fixed << showpoint << setprecision(2);
cout << "The average after the dropping the highest and lowest scores: " << result / 5 << endl;
return result;
}
// Function findLowest
double findLowest(double scores[])
{
double result = 10;
for (int x = 0; x < 7; x++)
{
if (scores[x] < result)
result = scores[x];
}
return result;
}
// Function findHighest
double findHighest(double scores[])
{
double result = 0;
for (int x = 0; x < 7; x++)
{
if (scores[x] > result)
result = scores[x];
}
return result;
}
Related
`
#include <iostream>
#include <iomanip>
using namespace std;
void getGrades(double g[], const int SIZE)
{
cout << "Please enter " << SIZE << " grades:" << endl;
for(int i = 0; i < SIZE; i++)
{
cin >> g[i];
}
}
double getAverage(double g[], const int SIZE)
{
int total = 0;
for(int i = 0; i < SIZE; i++)
{
total += g[i];
}
return total/SIZE;
}
void findDropInfo(double g[], const int SIZE, int &lowest, double average)
{
int total = 0;
lowest = g[0];
for(int i = 1; i < SIZE; i++)
{
if (lowest > g[i]) {
lowest = g[i];
}
}
average = (total - lowest)/SIZE;
return average;
}
void printData(double g[], int lowest, double average, double avg_before)
{
cout << "The 5 grades entered by the user are:" << endl;
cout << g[];
cout << "Grade dropped: " << lowest << endl;
cout << "Final Average: " << average << endl;
cout << "Average before drop: " << avg_before << endl;
}
// TODO: Complete the function definitions
int main()
{
const int SIZE = 5;
double grades[SIZE];
int lowest;
double avg,
avgBeforeDrop;
// TODO: Add function calls
getGrades(grades[SIZE], SIZE);
getAverage(grades[SIZE], SIZE);
findDropInfo(grades[SIZE], SIZE, lowest, avg);
printData(grades[SIZE], lowest, avg, avgBeforeDrop);
return 0;
}
`
Whenever I run the program, I get multiple errors saying there's no matching candidate function. I'm not sure if the problems are in the functions themselves or in the function calls, but from what I know the functions themselves should be fine. I'm also told there's an expected expression in g[] but I' not sure what's wrong there either, as it's meant to be empty.
Most issues have already been resolved in the comments, but note: cout << g[] does not print the elements of g.
The way to do this is
char separator = /* the character you want to use to separate the printed elements of g */
for (int i = 0; i < SIZE; i++)
{
cout << g[i] << separator;
}
if (separator != '\n') cout << '\n'; //this is to put the next print on the next line
I would put this as a comment but I don't have enough reputation :|
I want the program to ask the user to enter a number and then that function in the array will display on the screen
e.g
case 0 = displayNums (displays numbers entered by the user)
case 2 = getAverage (gets average of numbers entered)
I tried to code the menu to do that, but it only shows up. Nothing happens when the number for the specific function is entered.
#include <iostream>
#define integer 12
int ShowMenu(void);
double displayNums(double[], int);
double GetTotal(double[], int);
double getAverage(double[], int);
double getLargest(double[], int, int*);
double getSmallest(double[], int, int*);
int getNumOccurence(double[], int, int n);
double scaleUp(double[], int);
using namespace std;
int main() {
cout << "enter 12 integers 1 by 1:\n";
int data;
int n = 1;
// array to hold the integers
double arr[integer];
// get the integers
for (int i = 0; i < 12; i++) {
cin >> arr[i];
}
int option;
do {
option = ShowMenu();
switch (option) {
case 0:
double displayNums();
break;
case 1:
double GetTotal();
break;
case 2:
double getAverage();
break;
case 3:
double getLargest();
break;
case 4:
double getSmallest();
break;
case 5:
int getNumOccurence();
break;
case 6:
double scaleUp();
break;
case 7:
break;
default:
cout << "invalid option\n";
}
} while (option != 7);
// displays numbers entered by user
cout << displayNums(arr, integer) << endl;
// displays sum of numbers entered
cout << GetTotal(arr, integer) << endl;
// displays the average
cout << "Average integer is:" << getAverage(arr, integer) << endl;
// displays the largest
cout << "Largest integer is: " << getLargest(arr, integer, &data) << endl;
// display the smallest integer
cout << "Smallest integer is:" << getSmallest(arr, integer, &data) << endl;
// display the occurence of the num
cout << "Occurence integer is:" << getNumOccurence(arr, integer, n) << endl;
// display the scale up integers
cout << "Scaled up integers are:" << scaleUp(arr, integer) << endl;
return 0;
}
int ShowMenu(void) {
int option;
cout << "\t0. Display Numbers\n";
cout << "\t1. Get Total of numbers\n";
cout << "\t2. Get Average\n";
cout << "\t3. Get Largest\n";
cout << "\t4. Get Smallest\n";
cout << "\t5. Get Number Occurences\n";
cout << "\t6. Scale Up\n";
cout << "\t7. Quit\n";
cout << "\t\t\tOption ? ";
cin >> option;
return option;
}
double displayNums(double arr[], int size) {
double display = 0;
for (int i = 0; i < size; i++) { // for loop
}
cout << "the numbers that you have entered into the array are:" << endl;
for (int i = 0; i < size; i++) {
cout << arr[i] << endl; // displays numbers entered
}
return display;
}
double GetTotal(double arr[], int size) {
double sum = 0;
for (int i = 0; i < size; i++) {
}
cout << "" << endl;
for (int i = 0; i < 12; i++) {
sum += arr[i];
}
cout << "The sum of all numbers entered is:" << sum << endl;
return sum;
}
double getAverage(double arr[], int size) {
double sum = 0.0, avg;
for (int i = 0; i < size; i++) {
sum = sum + arr[i];
}
avg = sum / size;
return avg;
}
double getLargest(double arr[], int size, int* data) {
double large = 0;
for (int i = 0; i < size; i++) {
if (arr[i] > large) {
large = arr[i];
*data = i + 1;
}
}
return large;
}
double getSmallest(double arr[], int size, int* data) {
double small = 10000;
for (int i = 0; i < size; i++) {
if (arr[i] < small) {
small = arr[i];
*data = i + 1;
}
}
return small;
}
int getNumOccurence(double arr[], int size, int n) {
// only works when you insert repetable number 1
int count = 0;
for (int i = 0; i < size; i++) {
if (n == arr[i]) {
count++;
}
}
return count;
}
double scaleUp(double arr[], int size) {
int factor = 0;
cout << "enter the scale up factor";
cin >> factor;
for (int i = 0; i < size; i++) {
arr[i] *= factor;
cout << arr[i] << endl;
}
return factor;
}
You could use a std::map instead of all those cases.
// Declare a synonym for a function pointer
typedef double (*Function_Pointer)();
// Declare an abbreviation for the map:
typedef std::map<int, Function_Pointer> Function_Display_Table;
// Initialize the function table:
Function_Display_Table display_table;
display_table[0] = display_nums;
display_table[1] = GetTotal;
display_table[2] = getAverage();
//...
// To process your selection:
Function_Pointer fp = display_table.at(selection);
double return_value = fp();
To expand your menu selection, you only have to add rows to the display_table.
I'm working on homework for my c++ class we have just started arrays. I cannot figure out why the function reverse_array() does not print to the console when the function show_array() prints to the console without an issue. I've attempted googling the issue without success. I am just beginning c++ so it could be something small that I'm overlooking,. I appreciate any help.
#include<iostream>
using namespace std;
double dbval[5];
void fill_array(int x, double dbval[]);
void show_array(int x, double dbval[]);
void reverse_array(int x, double dbval[]);
int main(){
int x = 0;
fill_array(x,dbval);
show_array(x,dbval);
reverse_array(x,dbval);
return 0;
}
void fill_array(int x, double dbval[]){
int count = 0;
for (x = 0; x < 5; x++){
cin >> dbval[x];
if(!cin){
break;
}
}
for(x = 0; x < 5; x++){
count = count + 1;
}
cout << "Entries " <<int(count);
cout <<endl;
}
void show_array(int x, double dbval[]){
for (x = 0; x<5;x++){
cout << dbval[x] << " ";
}
cout << endl;
}
void reverse_array(int x, double dbval[]){
for(x = 5; x < 5; x--){
cout << dbval[x] << " ";
}
cout << endl;
}
This loop for(x = 5; x < 5 ; x--) failed because it did not satisfy your condition the first time it checked (x = 5, but it needs to be smaller than 5 to enter the loop)
Change to this:
void reverse_array(int x, double dbval[]){
for(x = 4; x >= 0 ; x--){
cout << dbval[x] << " ";
}
cout << endl;
}
I have this homework and i've completed this up to now. where i am stuck...
Basically i need to get the largest amount of rainfall and display it (which i already do have completed it) but also the number of the month.
This is where i am having an intense headache...
could you guys help me out with some code?
#include <iostream>
#include <iomanip>
using namespace std;
double yearlyRainAverage(double[], const int);
double smallestRainfall(double [], const int);
double largestRainfall(double [], const int);
int searchHighestMonth(double[], int);
int main() {
const int months = 12;
double inchesOfRain[months];
double sumOfAllMonths=0;
int maxMonthPosition = searchHighestMonth(inchesOfRain, months);
for (int count = 0; count < months; count++)
{
cout<<"Enter the rainfall (in inches) for month #"<< count + 1<<": ";
cin>>inchesOfRain[count];
sumOfAllMonths += inchesOfRain[count];
if(inchesOfRain[count] < 0){
cout <<"Rainfall must be 0 or more.\n";
cout<<"please re-enter: "<<endl;
cout<<"Enter the rainfall (in inches) for month #"<< count + 1<<": ";
cin>>inchesOfRain[count];
}
}
cout << fixed << showpoint << setprecision(2) << endl;
cout<<"the total rainfall for the year is "<<sumOfAllMonths<<" inches"<<endl;
cout<<"the average is "<<yearlyRainAverage(inchesOfRain, 12)<<" inches"<<endl;
// cout<<"The smallest amount of rainfall was: "<<smallestRainfall(inchesOfRain, 12)<<" inches ";
// cout<<"in month "<<(monthPosition+1)<<endl;
cout<<"The largest amount of rainfall was: "<<largestRainfall(inchesOfRain, 12)<<" inches ";
cout<<"in month "<<maxMonthPosition+1<<endl;
return 0;
}
double yearlyRainAverage(double inchesofrain[], const int months){
double sum=0;
for(int i=0;i<months; i++){
sum+=inchesofrain[i];
}
return sum/months;
}
double smallestRainfall(double inchesofrain[], const int months){
double smallest;
int i;
smallest=inchesofrain[0];
for(i=0; i < months; i++){
if(inchesofrain[i] < smallest){
smallest = inchesofrain[i];
}
}
return smallest;
}
double largestRainfall(double inchesofrain[], const int months){
double largest;
int i;
largest=inchesofrain[0];
for(i=0; i < months; i++){
if(inchesofrain[i] > largest){
largest = inchesofrain[i];
}
}
return largest;
}
Here is where i think is the issue. i think my logic is wrong. But, i am not sure.
int searchHighestMonth(double inchesofrain[], int value){
int max = 0;
for ( int i=1; i < value; ++i) {
if ( inchesofrain[max] < inchesofrain[i] ) {
max = i;
}
}
return max;
}
The problem is that you are searching for your largest rainfall before you have taken the input of your rainfall from the user.
Move this line:
int maxMonthPosition = searchHighestMonth(inchesOfRain, months);
After the input for loop.
I went ahead and tested all of your code again, redirecting stdin (cin) from an input string, which I find very helpful for testing so I don't have to keep inputting. here is my code:
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
double yearlyRainAverage(double[], const int);
double smallestRainfall(double [], const int);
double largestRainfall(double [], const int);
int searchHighestMonth(double inchesofrain[], int value) {
int max = 0;
for (int i = 1; i < value; ++i) {
if (inchesofrain[max] < inchesofrain[i]) {
max = i;
}
}
return max;
}
int main() {
//#define testing // comment this line out to use std::cin for input
#ifdef testing
// code to get stdin input from a local buffer
std::string input_string{"4 5 6 7 8 9 10 3 2 3 4 5"};
std::streambuf *orig = std::cin.rdbuf();
std::istringstream input(input_string);
std::cin.rdbuf(input.rdbuf());
#endif
const int months = 12;
double inchesOfRain[months];
double sumOfAllMonths = 0;
for (int count = 0; count < months; count++) {
cout << "Enter the rainfall (in inches) for month #" << count + 1 << ": " << std::endl;
cin >> inchesOfRain[count];
sumOfAllMonths += inchesOfRain[count];
while (inchesOfRain[count] < 0) {
cout << "Rainfall must be 0 or more.\n";
cout << "please re-enter: " << endl;
cout << "Enter the rainfall (in inches) for month #" << count + 1 << ": " << std::endl;
cin >> inchesOfRain[count];
}
}
int maxMonthPosition = searchHighestMonth(inchesOfRain, months);
cout << fixed << showpoint << setprecision(2) << endl;
cout << "the total rainfall for the year is " << sumOfAllMonths << " inches" << endl;
cout << "the average is " << yearlyRainAverage(inchesOfRain, months) << " inches" << endl;
// cout<<"The smallest amount of rainfall was: "<<smallestRainfall(inchesOfRain, 12)<<" inches ";
// cout<<"in month "<<(monthPosition+1)<<endl;
cout << "The largest amount of rainfall was: " << largestRainfall(inchesOfRain, 12) << " inches ";
cout << "in month " << maxMonthPosition + 1 << endl;
#ifdef testing
std::cin.rdbuf(orig);
#endif
return 0;
}
double yearlyRainAverage(double inchesofrain[], const int months) {
double sum = 0;
for (int i = 0; i < months; i++) {
sum += inchesofrain[i];
}
return sum / months;
}
double smallestRainfall(double inchesofrain[], const int months) {
double smallest;
int i;
smallest = inchesofrain[0];
for (i = 0; i < months; i++) {
if (inchesofrain[i] < smallest) {
smallest = inchesofrain[i];
}
}
return smallest;
}
double largestRainfall(double inchesofrain[], const int months) {
double largest;
int i;
largest = inchesofrain[0];
for (i = 0; i < months; i++) {
if (inchesofrain[i] > largest) {
largest = inchesofrain[i];
}
}
return largest;
}
Your searchHighestMonth() function is almost good. But its loop must start at 0 not 1, like in the rest of your code. Loop is from 0 to 11 if it is a full year with 12 months. Besides, it is more readable if you store the current maximum rain value in some variable.
int searchHighestMonth(double inchesofrain[], int monthcount){
double maxrain = -1.0;
int max = -1;
for ( int i=0; i < monthcount; ++i) {
if ( maxrain < inchesofrain[i] ) {
maxrain = inchesofrain[i];
max = i;
}
}
return max;
}
Side remark: In actual production code, you would probably want to use std::vector objects, which maintain their own size, rather than plain old C-style arrays.
Side remark: if you want to give your user a second chance to enter a proper non-negative rainfall value, you must do it before including that value into the sum.
I am trying to have the program output the winner in a candidate race when trying to go through the array to get the highest count instead of the highest it gets the next highest from zero, and i feel like i have tried everything.
I keep trying to change things around in the find winner function but nothing seems to be working I dont see what i am doing wrong please help.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
//User inputs data for candidates and votes.
//Output Candidates, votes, and percentage.
//Names that will be used are johnson, miller, duffy, robinson, ashtony
//count of votes to use = 5000, 4000, 6000, 2500, 1800, total 19300
//Percentages that will be used for candidates= 25.91, 20.73, 31.09, 12.95, 9.33
int findWinner(int votes[]);
void Results(string candidates[], int votes[]);
double Percentage(int votes[], int vote);
int tester[5] = {};
const int NUMBER_OF_CANDIDATES = 5;
int main()
{
string candidates[NUMBER_OF_CANDIDATES];
int votes[NUMBER_OF_CANDIDATES];
cout << "Enter 5 Candidates with their votes ex: DelBosque 7000: ";
for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
cin >> candidates[i] >> votes[i];
}
cout << "Candidate Votes Received % of Total Votes" << endl;
Results(candidates, votes);
cout << "The Winner of the Election is " << candidates[findWinner(votes)] <<
endl;
return 0;
}
double Percentage(int votes[], int vote){
int sumOfVotes = 0;
for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
sumOfVotes += votes[i];
}
double percent = static_cast<double>(vote) / sumOfVotes;
double votePercent = 0;
votePercent = percent * 100;
std::cout << std:: fixed;
std::cout << std:: setprecision(2);
std::cout << votePercent;
return 0;
};
void Results(string candidates[], int votes[]){
for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
cout << candidates[i] << setw(15) << votes[i] << setw(15);
int percent = Percentage(votes, votes[i]);
cout << percent << "%" << endl;
};
};
// You are returning the number of votes. Shouldn't you be returning the
// index referenced by the highest number of votes?
int findWinner(int votes[]){
int index = 0;
int winner = 0;
for (int i = 0; i < NUMBER_OF_CANDIDATES; i++) {
if (votes[i] > winner)
winner = votes[i];
index = i;
};
return index;
};
You need both the following lines under the if condition.
winner = votes[i];
index = i;
like:
if (votes[i] > winner)
{
winner = votes[i];
index = i;
}
What you have is equivalent to:
if (votes[i] > winner)
{
winner = votes[i];
}
index = i;
which is not correct.