How to print index number of an array element? - c++

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.

Related

How can I alter my program for the desired output?

I am trying to write a program that takes user input for the number of hours worked by 6 employees of a company to store in a two-dimensional array and prints the output in the following format:
Employee Number Hours
01 a
02 b
03 c
04 d
05 e
06 f
where a-f would be the user entered integer values.
I have experimented with several methods but am unable to get my desired output. I am attaching the recent attempt:
#include <iostream>
using namespace std;
int main()
{
int array[6][2]; // 6 by 2 matrix to store employee name and hours worked
int i, j;
cout << "Enter the number of hours worked by 6 employees: " << endl;
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 2; j++)
{
cin >> array[i][j];
cout << array[i][j] << "\t" << endl;
break;
}
cout << endl;
}
}
What changes should I make to get the required output?
You should take the input first. Then in order to print with column name you should first print those column names and then print the values.
Your code should be like this -
#include <iostream>
using namespace std;
int main()
{
int array[6][2]; // 6 by 2 matrix to store employee name and hours worked
int i, j;
cout << "Enter the number of hours worked by 6 employees: " << endl;
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 2; j++)
{
cin >> array[i][j];
}
}
cout << "Employee Number \t Hours" << endl;
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 2; j++)
{
cout << array[i][j] << "\t\t\t ";
}
cout << endl;
}
}
Output:
Edit:
For your requirement from the comment, new code will be
int main()
{
int array[6][2]; // 6 by 2 matrix to store employee name and hours worked
int i, j;
cout << "Enter the number of hours worked by 6 employees: " << endl;
cout << "Employee Number \t Hours" << endl;
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 2; j++)
{
cin >> array[i][j];
cout << array[i][j] << "\t\t\t ";
}
cout << endl;
}
}

c++ Output only the max array value and count

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;
}

Result not showing as I want to (C++ code)

I'm a novice in C++ so don't really know much but I want to learn and practice. I've written a code where the user will state how many persons he/she wants to write the names, and the names will be in char. I will then have to sort this using insertion to see which name is well sorted Alphabetically.
#include <iostream>
using namespace std;
class Name{
public:
void sortInsertion(int, char* );
};
void Name::sortInsertion(int size, char *c){
for (int i = 1; i < size - 1; i++){
int j;
j = i;
while (j > 0 && c[j] < c[j - 1]){
swap(c[j], c[j - 1]);
j = j - 1;
}
}
}
int main(){
Name n;
char* name;
name = new char[30];
int* in;
int size = 0;
in = new int[size];
cout << "How many people?: ";
cin >> size;
for (int i = 0; i < size; i++){
cout << "Person " << i+1 <<": "<< endl;
cout << "Full Name: ";
cin.ignore();
cin.getline(name,29);
cout << endl;
}
n.sortInsertion(size, name);
for (int i = 0; i < size; i++){
cout << "Person " << i + 1 << ": " << endl;
cout << "Full Name: ";
cout << name << endl;
}
system("pause");
}
this is my code. It doesn't show the result as I want it to show, the output misses the first letter of the name and only repeats the printing of the last user input given.
Could you please tell me what's wrong? Any help and tip is really appreciated :)

How to compare the value inside of an array

So Im writing a program that asks the user to input the number of pancakes a person(1-10) had for breakfast. The program must analyze the input and determine which person ate the most pancakes. Also the program must output a list in order of number of pancakes eaten of all 10 people. So far I have written the code to get the user input and the code to display the array, but not in order. Im completely lost when it comes to comparing the elements in the array:
int getPancakes();
void displayArray(int theArray[],int sizeOfArray);
void compareArray(int sizeOfArray);
int pancakes[10];
int z = 0;
int main()
{
}
int getPancakes(){
int y;
int x = 0;
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){
for(int x = 0 ;x < sizeOfArray ; x++){
++z;
cout << "Person " << z << " ate " << pancakes[x] << " pancakes" << endl;
}
}
So how can I instruct my program to compare the elements inside the array? Also how can I instruct my program to print the list of number of pancakes eaten by each person in order?
In order to find who ate the most pancakes, you basically need to find the position of the maximum value in the array.
int findMaxPosition(int array[], int arraySize){
int maxPosition = 0; //assume the first element is maximum
for(int i = 1; i < arraySize; i++)
if(array[i] > array[maxPosition]) //compare the current element with the known max
maxPosition = i; //update maxPosition
return maxPosition;
}
Note that this gives you the first occurence of the maximum value. If the elements are unique, that's enugh. Otherwise, you should find the maximum value, array[maxPosition], and iterate through the array and display every position on which it occurs.
Sorting is a little bit complicated. The sorting algorithms aren't so straightforward, and I'm afraid that if I write you an implementation, I wouldn't help you.
One of the simplest sorting algorithms is bubble sort. Wikipedia (http://en.wikipedia.org/wiki/Bubble_sort) has a detailed page about it, and you should be able to implement it using the pseudocode given there.
If all numbers are unique
int max = 0;
for(int x = 0 ;x < 10 ; x++)
{
if(pancakes[x] > max)
max = pancakes[x];
}
for(int x = 0 ;x < 10 ; x++)
{
if(pancakes[x] == max)
cout << "Person " << x << " ate " << pancakes[x] << " pancakes - biggest number" << endl;
}
To be blunt, there are two methods for comparing an element of an array to another value, direct and through a copy.
// Make a copy:
int count = pancakes[x];
if (count == limit)
{
//...
}
// Direct access
if (pancakes[x] == limit)
{
//...
}
There is no need to run through the array looking for max then sorting to provide output. Instead, you can just keep track of the max value you have found during the input phase:
int getPancakes(){
int max = 0;
for(int y = 0; y < 10; y++){
cout << "How many pancakes did person " << y << " eat?" << endl;
cin >> pancakes[y];
if (pancakes[y]>pancakes[max]){
max = y;
}
}
return max;
}
Note that I removed the redundant declaration of y (you are declaring it in the for loop) and x (was always going to be equal to y).
I also added a return to the function (index of the person who has eaten the most) as you had no return value. (Either return something or make the function return void (no return)).
If you only care about the max number eaten, then you don't even need to keep track of max. Instead, just read the largest value from the array after the sorting step.
Now all you need to do is implement void sortArray() and call it before calling the display function:
int main()
{
int max = getPancakes();
sortArray(); //left to you to implement
displayArray(pancakes,10);
}
You may want to consider making pancakes local to main and passing it into your functions in the same way that you are doing displayArray.
I just finished coming up with a solution to the same exercise, regarding the "who ate the most" part, even if there is more than one person who ate the most pancakes. It involves arrays and vectors. Tested and working:
#include <iostream>
#include <array>
#include <vector>
using namespace std;
int main()
{
array <int, 10> PancakeEater;
vector<int> Winners;
int max;
cout << "Type number of pancakes eaten, separated by spaces, for each of the ten contestants:" << endl;
cout << "#1 #2 #3 #4 #5 #6 #7 #8 #9 #10" << endl;
for (int i = 0; i < PancakeEater.size(); i++)
{
cin >> PancakeEater[i];
}
max = PancakeEater[0];
Winners.push_back(1);
for (int i = 0; i < PancakeEater.size()-1; i++)
{
if (max == PancakeEater[i + 1])
{
Winners.push_back(i + 2);
}
if (max < PancakeEater[i + 1])
{
max = PancakeEater[i + 1];
Winners.clear();
Winners.shrink_to_fit();
Winners.push_back(i + 2);
}
}
if (Winners.size() > 1)
{
cout << endl << "The contestants that ate the most pancakes, on a tie for first place, are contestant numbers " << endl;
for (auto item : Winners)
{
cout << item << " ";
}
}
else
{
cout << endl << "The contestant that ate the most pancakes is contestant number " << endl;
for (auto item : Winners)
{
cout << item << " ";
}
}
}

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");
}