c++ Output only the max array value and count - c++

I was comparing the array values in format arr[i] > arr[max]
It outputs all the values which is greater to arr[0].
Can you guys give me tips on how to ouput JUST the greatest value?
Code:
for(int i=0;i<4;i++){
cout<<"Enter how many pancakes eaten by the Person #"<<i<<" : ";
cin>>pancake;
person[i] = pancake;
}
for(int i=0;i<4;i++){
if(person[i]>person[i+1]){
pancakeCount = pancake[i];
cout<<"The number of most eaten pancakes is "<<pancakeCount<<" by Person#"<<i<<endl;
}
}
The problem is the output:
Output:
//list of persons and the number of pancakes they ate.
The number of most eaten pancakes is 10 by Person#7
The number of most eaten pancakes is 10 by Person#9
each time I put the greatest value before the last iteration, it also outputs the last iteration containing the greatest value which is Person#7
I only want the highest value to be the output itself.

int pancakeCount(0), max_person(0);
for(int i=0;i<4;i++){
cout<<"Enter how many pancakes eaten by the Person #"<<i<<" : ";
cin>>pancake;
person[i] = pancake;
}
for(int i=0;i<4;i++){
if(person[i]>pancakeCount){
pancakeCount = pancake[i];
max_person = i;
}
}
cout<<"The number of most eaten pancakes is "<<pancakeCount<<" by Person#"<<i<<endl;

Hi your code is wrong algorithim try mine
for(int i=0;i<4;i++)
{
std::cout<<"Enter how many pancakes eaten by the Person #"<<i<<" : ";
std::cin>>pancake;
person[i] = pancake;
}
for(int i=0;i<4;i++)
{
if(person[i]>pancakeCount)
{
pancakeCount = person[i];
personWhoEatMost = i;
}
}
std::cout<<"The number of most eaten pancakes is "<<pancakeCount
<<" by Person#"<<personWhoEatMost<<std::endl;

split the loops for finding the max and printing the max.
Code:
#include <iostream>
using namespace std;
int main(){
int person[4];
int pancake, pancakeMax, pancakeMin;
for (int i = 0; i<4; i++)
{
cout << "Enter how many pancakes eaten by the Person #" << i << " : ";
cin >> pancake;
person[i] = pancake;
}
//equate to one of the elements to make sure negative numbers are accomodated
pancakeMax = pancakeMin = pancake;
// most eaten
for (int i = 0; i<4; i++)
{
if (person[i]>pancakeMax)
{
pancakeMax = person[i];
}
}
for (int i = 0; i<4; i++)
{
if (person[i] == pancakeMax)
{
cout << "The number of most eaten pancakes is " << pancakeMax << " by Person#" << i << endl;
}
}
//least eaten
for (int i = 0; i<4; i++)
{
if (person[i]<pancakeMin)
{
pancakeMin = person[i];
}
}
for (int i = 0; i<4; i++)
{
if (person[i] == pancakeMin)
{
cout << "The number of least eaten pancakes is " << pancakeMin << " by Person#" << i << endl;
}
}
return 0;
}
You can add a break statement in the second loop if you think your logic needs just one value. Otherwise, right now, it give all occurances of maximum pancakes eaten.
If you are targetting efficiency you can merge the loops like so:
#include <iostream>
using namespace std;
int main(){
int person[4];
int pancake, pancakeMax, pancakeMin;
int iMax, iMin;
for (int i = 0; i<4; i++){
cout << "Enter how many pancakes eaten by the Person #" << i << " : ";
cin >> pancake;
person[i] = pancake;
}
//equate to one of the elements to make sure negative numbers are accomodated
pancakeMax = pancakeMin = pancake;
for (int i = 0; i<4; i++){
pancake = person[i];
if (pancake>pancakeMax){// most eaten
pancakeMax = pancake;
iMax = i;
}
if (pancake < pancakeMin){//least eaten
pancakeMin = pancake;
iMin = i;
}
}
cout << "The number of most eaten pancakes is " << pancakeMax << " by Person#" << iMax << endl;
cout << "The number of least eaten pancakes is " << pancakeMin << " by Person#" << iMin << endl;
return 0;
}

Related

Cannot convert int to int[][]

I am doing a a problem using Multi-dimensional Arrays. I call the function in main but I get this Error message. This is the Original Problem I have:
A local zoo wants to keep track of how many pounds of food each of its three tigers eats each day during
a typical week. Write a program that stores this information in a two-dimensional 3 x 7 array, where
each row represents a different tiger and each column represents a different day of the week. The
program should first have the user input the data for each tiger. Then it should create a report that
includes the following information:
Average amount of food eaten by each of the tigers during the week.
Average amount of food eaten by all the tigers per day for each day.
The least amount of food eaten during the week by any tiger.
The greatest amount of food eaten during the week by any tiger.
the code:
#include <iostream>
using namespace std;
const int TIGERS = 3;
const int DAYS = 7;
int food[TIGERS][DAYS];
void inputArray(int food[TIGERS][DAYS]);
float AvgTig(float);
float Least(float);
float most(float);
int main()
{
float maximum;
float minimum;
float total = 0.0f;
float average = 0.0f;
float result = 0;
inputArray(food);
maximum = food[0][0];
minimum = food[0][0];
cout << "\n \n";
AvgTig(result);
cout << "\n \n";
Least(minimum);
cout << "\n \n";
most(maximum);
cout << "\n \n";
system("PAUSE");
return 0;
//end program
}
void inputArray(int food[TIGERS][DAYS])
{
int total = 0;
for (int tig = 0; tig < TIGERS; tig++) {
for (int day = 0; day < DAYS; day++) {
cout << "TIGER " << (day + 1) << ", day " << (day + 1) << ": ";
cin >> food[tig][day];
}
cout << endl;
}
}
float AvgTig(float output)
{
int total = 0;
for (int i = 0; i < TIGERS; i++) {
for (int day = 0; day < DAYS; day++) {
total += food[i][day];
}
cout << endl;
}
output = total / (TIGERS * DAYS);
cout << "Average food for Tigers in the days is :" << output << " ";
return output;
}
float Least(float minimum)
{
for (int least = 0; least < TIGERS; least++) {
for (int day = 0; day < DAYS; day++) {
if (food[least][day] < minimum)
minimum = food[least][day];
}
}
cout << "Minimum food eaten: " << minimum << " ";
return minimum;
}
float most(float maximum)
{
for (int most = 0; most < TIGERS; most++) {
for (int day = 0; day < DAYS; day++) {
if (food[most][day] > maximum)
maximum = food[most][day];
}
}
cout << " Maximum number of food is: " << maximum << " ";
return maximum;
}
On this line:
inputArray(food[TIGERS][DAYS]);
in main, you are calling inputArray with a single int, and not the entire food array. In fact, even reading this position in food is UB.
You need to call the function like this:
inputArray(food);

I am not getting the lowest selling product in output.How can i correct it? in last line is output for lowest selling product.Thank you

The program should prompt the user to enter the number of jars sold for each type.The program should produce a report that displays sales for each salsa type, total sales, and the names of the highest selling and lowest selling products.
#include <iostream>
#include<string>
using namespace std;
int main()
{
const int SIZE=5;
string salsa_names[SIZE] = { "mild","medium","sweet","hot","zesty" },name1,name2;
double number_of_jars_sold[SIZE],total=0;
cout << "enter the number of jars sold for each of different types of salsa\n";
for (int i = 0; i < SIZE; i++)
{
cout <<"The salsa names are :"<<"'"<<salsa_names[i]<<"'";
cin >> number_of_jars_sold[i];
total += number_of_jars_sold[i];
}
double large=number_of_jars_sold[0],small=number_of_jars_sold[0];
cout << "The sales for each of salsa type is ="<<endl;
for (int i=0;i<SIZE;i++)
{
cout << salsa_names[i] << " : " << number_of_jars_sold[i] << endl;
}
cout << "total sale is equal to" << total << endl;
for (int i = 0; i < SIZE; i++)
{
if (large < number_of_jars_sold[i])
{
large = number_of_jars_sold[i];
name1=salsa_names[i];
}
}
for (int j = 0; j < SIZE; j++)
{
if (small > number_of_jars_sold[j])
{
small = number_of_jars_sold[j];
name2 = salsa_names[j];
}
}
cout << "The name of highest selling product is : " << name1 << endl;
cout << "The name of lowest selling product is : " << name2 << endl;
return 0;
}
Edited::
you should initialize small and large to zero first, add an if condition before this line if (small > number_of_jars_sold[j]):
if(small == 0), so you will have minimum value in small variable.

Using arrays and strings together

So I'm trying to create an array that contains some user inputted names, and then associate those names with letter grades from tests (ex: A, B, C, D, F). My question is, how would I use an array to accept the user inputted names?
EDIT:
Sorry this is a bit long, I don't know what part to put that would help out. Totally new to C++ and I can't seem to find anything online regarding the matter, lol.
Here is some code. This program currently asks the user for test scores, then displays and drops the lowest test score, and finally, calculates the average of the scores without the lowest one. The end goal is to ask the user for 5 students names, and 4 scores for each student, then dropping the lowest score for each student and calculating the averages of ALL scores inputted regardless of student.
#include <iostream>
#include <string>
using namespace std;
void getScore(int &);
int findLowest(int [], int);
void calcAverage(int [], int);
int main () {
const int NUM_SCORES = 5;
int scores[NUM_SCORES];
cout << "Welcome to test averages." << endl;
cout << "Please enter scores for " << NUM_SCORES << " students." << endl;
cout << endl;
for (int i = 0; i < NUM_SCORES; i++) {
getScore(scores[i]);
}
for (int i = 0; i < NUM_SCORES; i++) {
cout << "Score " << (i + 1) << ": " << scores[i] << endl;
}
cout << endl;
cout << "The lowest of these scores is " << findLowest(scores, NUM_SCORES) << endl;
calcAverage(scores, NUM_SCORES);
return 0;
}
void getScore(int & s) {
s = -1;
cout << "Please enter a test score: ";
cin >> s;
while (s < 0 || s > 100) {
cout << "Score range must be from 0-100" << endl;
cout << "Please re-enter a score: ";
cin >> s;
}
}
int findLowest(int theArray [], int theArraySize) {
int lowest = theArray[0];
for (int i = 1; i < theArraySize; i++) {
if (theArray[i] < lowest) {
lowest = theArray[i];
}
}
return lowest;
}
void calcAverage(int theArray [], int theArraySize) {
int sum = 0;
for (int i = 0; i < theArraySize; i++) {
sum += theArray[i];
}
double average = (sum - findLowest(theArray, theArraySize)) / (theArraySize - 1.0);
cout << "The average is " << average << endl;
}
Try getline from #include <string>
std::string names[5];
for (int i = 0; i < 5; ++i){
getline(std::cin, names[i]);
}

How to print index number of an array element?

So Im doing exercise where I have to ask the user to input the number of pancakes eaten by 10 people, print out who ate the most pancakes and then organize the list from greatest to smallest, basically I have to print out:
Person 3: Ate 10 pancakes , Person 5: Ate 9 pancakes ,Person 8: Ate 8 pancakes
However after using bubble sort I cant match the person with the right value, because bubble sort swaps them! Does anyone know of any way to fix this? For example is there another way to organize the values in an array without using bubble sort?
void getPancakes()
{
int x = 0;
int temp;
for(int y = 0; y < 10; y++)
{
++x;
cout << "How many pancakes did person " << x << " eat?" << endl;
cin >> pancakes[y];
}
}
void displayArray(int theArray[],int sizeOfArray)
{
int temp;
int i,j;
int q = 10;
for(i = 0; i <= sizeOfArray - 1 ; i++)
{
for(j = i+1 ; j < sizeOfArray ; j++)
{
if(theArray[i] < theArray[j])
{
temp = theArray[i];
theArray[i] = theArray[j];
theArray[j] = temp;
}
}
}
cout << endl;
for(i = 0; i < 10; i++)
cout << "Person " << i+1 << " ate " << theArray[i] << " pancakes" << endl;
}
Like Cody said, you'll have to store both values. As #HgMs indicated you could you a class, or a struct, which is a class with only public data members. Here's an example:
struct person{
int id;
int pancakes;
};
int main(){
const int totalPeople = 10;
person personArray[totalPeople];
for (int i = 0; i < totalPeople; ++i){
cout << "How many pancakes did person " << i << " eat? ";
personArray[i].id = i;
cin >> personArray[i].pancakes;
}
for (int i = 0; i < totalPeople; ++i){
cout << "id: " << personArray[i].id << "\t" << "Pancakes eaten: " << personArray[i].pancakes << endl;
}
return 0;
}
You can then iterate over the array and look at the number of pancakes each person has eaten using the '.' to access the property.
EDIT: A bubble sort would work fine.

Pancake glutton program. (C++)

Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
arrays
Write a program that asks the user to enter the number of pancakes eaten for breakfast by 10 different people (Person 1, Person 2, ..., Person 10)
Once the data has been entered the program must analyze the data and output which person ate the most pancakes for breakfast.
★ Modify the program so that it also outputs which person ate the least number of pancakes for breakfast.
★★★★ Modify the program so that it outputs a list in order of number of pancakes eaten of all 10 people.
i.e.
Person 4: ate 10 pancakes
Person 3: ate 7 pancakes
Person 8: ate 4 pancakes
...
Person 5: ate 0 pancakes
Current version I have written: http://codepad.org/QHnt11CT
#include <iostream>
#include <string>
void bubbleSort(int arr[], int n) {
bool swapped = true;
int j = 0;
int tmp;
while (swapped) {
swapped = false;
j++;
for (int i = 0; i < n - j; i++) {
if (arr[i] > arr[i + 1]) {
tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
swapped = true;
}
}
}
}
int main()
{
int pancakeAmount[10];
std::string consumers[10];
for (int i = 0, j = 1; i < 10; i++, j++) {
std::cout << "Please enter an amount of pancakes eaten by consumer"\
" number " << j << "." << std::endl;
std::cin >> pancakeAmount[i];
std::cout << "Please enter the name of the person who ate that amount"\
" of pancakes." << std::endl;
getline(std::cin, consumers[i]);
}
std::cout << "The results from least amount eaten to the greatest amount"\
" eaten are as follows:" << std::endl;
bubbleSort(pancakeAmount, 10);
for (int k = 0; k < 10; k++) {
std::cout << pancakeAmount[k] << std::endl;
}
return 0;
}
That is the problem I am currently working on. As of right now, the first two objectives of this problem have been solved without any notable issues.
The third objective, however, is proving to be a bit more difficult. I am having a hard time designing/implementing a sorted list with the appropriate labels. In line 47 I am trying to obtain a name or label to give to the corresponding amount.
I am running into an issue where the console will accept the amount I want to assign, but will completely ignore the call to the getline() function and loop back into asking for another amount. When the getline() function is called before the "std::cin >> pancakeAmount[i]" is called, I can give input on the first loop, but successive loops produce the error I was encountering when the getline() function was in it's original position in the code.
Am I trying to utilize an array of strings in an improper fashion, or is the getline() function not being used properly?
So today is your lucky day, because usually I don't do homework.
Your problem is that you need to ignore the return of your cin.
So this should solve your problem but it is not tested:
struct PanCakeEater
{
int pancakeEaten;
std::string name;
};
bool PanCakeSort(PanCakeEater const& eater1, PanCakeEater const& eater2)
{
return eater1.pancakeEaten < eater2.pancakeEaten;
}
int main()
{
std::vector<PanCakeEater> eaters;
const size_t numberOfEaters = 3;
for(size_t i = 0; i < numberOfEaters; ++i)
{
PanCakeEater p;
std::cout << "Please enter an amount of pancakes eaten by consumer number " << i << "." << std::endl;
std::cin >> p.pancakeEaten;
std::cin.ignore(); // ignore the \n
std::cout << "Please enter the name of the person who ate that amount of pancakes." << std::endl;
getline(std::cin, p.name);
eaters.push_back(p);
}
std::sort(eaters.begin(), eaters.end(), PanCakeSort);
for(auto iter = eaters.begin(); iter != eaters.end(); ++iter)
{
std::cout << iter->name << ": " << iter->pancakeEaten << std::endl;
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
 int ate[10],eater[10],hold,temp;
 cout<<"\t\tEnter How Many Pancakes Did Each Person Eat?\n\n";
 for(int h=0;h<10;h++)
 {
  eater[h]=h;
 }
 for(int q=0;q<10;q++)
 {
  cout<<"Person "<<eater[q]+1<<" Ate: ";
  cin>>ate[q];
 }
 cout<<"____________________________________________\n\n"<<"\t\tOrdered  List Of Above.\n\n";
 for(int a=0;a<10;a++)
 {
  for(int b=0;b<10;b++)
  {
   if(ate[b]<ate[b+1])
   {
    hold=ate[b];
    ate[b]=ate[b+1];
    ate[b+1]=hold;
    temp=eater[b];
    eater[b]=eater[b+1];
    eater[b+1]=temp;
   }
  }
 }
 for(int w=0;w<10;w++)
 {
  cout<<"Person "<<eater[w]+1<<" Ate "<<ate[w]<<" Pancakes\n";
 }
 return 0;
}
Here is a simple version of it. Must be easier for some beginners to understand.
#include <iostream>
#include <string>
using namespace std;
int main() {
int y;
cout << "How many people do you want to enter= \n";
cin >> y;
string names[10];
int pancakes[10];
for (int i = 0; i <= y-1 ; i++) {
cout << "enter name= ";
cin >> names[i];
cout << "num of pancakes= ";
cin >> pancakes[i];
}
int maxEaten = pancakes[0];
int minEaten = pancakes[0];
string maxPer = names[0];
string minPer = names[0];
for (int k = 1; k < y; k++) {
if (pancakes[0] < pancakes[k]) {
maxEaten = pancakes[k];
maxPer = names[k];
}
if (pancakes[0] > pancakes[k]) {
minEaten = pancakes[k];
minPer = names[k];
}
}
cout << maxPer << " ate " << maxEaten << " which is the maximum." << endl;
cout << minPer << " ate " << minEaten << " which is the minimum." << endl;
system("pause");
}