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 9 years ago.
Improve this question
This is the program that I need to solve:
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.
Could anybody write the code for returning the index of biggest value (I have labeled it as "h")
#include "iostream"
using namespace std;
int main()
{
int x[11];
int y;
int h;
for (int i = 1; i <= 10; i++)
{
cin >> i[x];
cout << "Person: " << i << " has eaten " << i[x] << " pancakes" << endl
y = x[0];
h = x[0];
for (int j = 1; j <= 10; j++)
{
if (x[j] > y)
{
y = x[j];
}
}
}
cout << "The most pancakes are eaten by Person " << h << " with " << y << endl;
system("pause");
return 0;
}
Not tested, should work:
#include <iostream>
using namespace std;
int main()
{
int x[11];
int ans, ansmax = 0;
for (int i = 1; i <= 10; i++)
{
cin >> x[i]; // You had wrong variable here
cout << "Person: " << i << " has eaten " << x[i] << " pancakes" << endl
if(x[i] > ansmax)
{
ansmax = x[i];
ans = i;
}
}
cout << "The most pancakes are eaten by Person " << ans << " with " << ansmax << endl;
system("pause");
return 0;
}
My five cents
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
const int N = 10;
int person[N];
// Entering initial data
for ( int i = 0; i < N; i++ )
{
cout << "Enter number of pancakes eaten by person " << i + 1 << ": ";
cin >> person[i];
}
// Finding the index (favorite) of the person who has eaten the most pancakes.
int favorite = 0;
for ( int i = 1; i < N; i++ )
{
if ( person[favorite] < person[i] ) favorite = i;
}
// Now all is ready to show the result
cout << "\nThe most pancakes are eaten by Person " << favorite + 1
<< " with " << person[favorite] << endl;
system( "pause" );
return 0;
}
#include <algorithm>
int index = std::max_element(x, x + 11) - x;
Related
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 months ago.
Improve this question
i know Matlab as a beginner. It works in Matlab but not working in c++.
#include <iostream>
using namespace std;
int main()
{
int birinci, degerbir, sonuc, degeriki, degeruc, degerdort, degerbes;
int opr = 144;
int basamaksayisi;
int i;
cout << "kac basamakli olsun" << endl;
cin >> basamaksayisi;
while (basamaksayisi != i)
{
cout << "birinci degeri giriniz";
cin >> degerbir;
cout << "deger 1:" << degerbir << endl;
sonuc = degerbir;
i = 1;
cout << i;
cout << "ikinci deger giriniz";
cin >> degeriki;
degeriki = degeriki * 10;
sonuc = degerbir + degeriki;
i = 2;
cout << i;
cout << "ucuncu deger giriniz";
cin >> degeruc;
degeruc = degeruc * 100;
sonuc = degeruc + sonuc;
i = 3;
cout << i;
cout << "4.deger giriniz";
cin >> degerdort;
degerdort = degerdort * 1000;
sonuc = degerdort + sonuc;
i = 4;
cout << i;
cout << "5.deger giriniz";
cin >> degerbes;
degerbes = degerbes * 10000;
sonuc = degerbes + sonuc;
i = 5;
cout << i;
}
cout << "degerler sunlardır:" << degerbir << endl << degeriki << endl << degeruc << endl << degerdort << endl << degerbes << endl;
cout << "degeler toplami" << sonuc;
return 0;
}
why this "i" and other math operations etc. not counting in while loop ? also similar if else loop not working. Just in and count functions working.
i was tried change while loop to if loop. but its not working. loop's inside math processing not working
Here is output.
sh -c make -s
./main
kac basamakli olsun
3
birinci degeri giriniz3
deger 1:3
1ikinci deger giriniz4
2ucuncu deger giriniz5
34.deger giriniz2
45.deger giriniz1
5birinci degeri giriniz4
deger 1:4
1ikinci deger girinizsignal: terminated
Look at this code
int i;
cout<<"kac basamakli olsun"<<endl;
cin>>basamaksayisi;
while(basamaksayisi!=i)
What do you think the value of i is here?
No idea about MatLab but in C++ the variable i has not been given a value before it is used, which automatically means that this code is bugged.
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 4 months ago.
Improve this question
I've made a code about Bank card, does it want silver or gold, then calculate the saving interests in 12 months,The output should be 112682512 if we input the saldoAwal = 100000000, but somehow it shows 101000000
What should I do?
I'm so confused because I've checked the calculations and still wrong, Help me?
THANKS
#include <iostream>
#include <iomanip>
using namespace std;
struct Orang
{
string namaNasabah = "";
int jenisTabungan = 0;
int saldoAwal = 0;
int saldoBaru = 0;
};
Orang orang[100];
int n, i = 0;
float saldoMin, bunga, biayaAdministrasi;
void bungaTahunan(Orang orang[100]);
int main()
{
cout << "Banyak nasabah : ";
cin >> n;
for(int i = 0; i < n ; i++)
{
cout << "Nama nasabah : ";
cin >> orang[i].namaNasabah;
cout << "Pilih silver atau gold? (1/2)"<<endl;
cin >> orang[i].jenisTabungan;
cout << "Saldo awal anda : ";
cin >> orang[i].saldoAwal;
bungaTahunan(&orang[i]);
}
cout << "nomor" << setw(20) << "nama" << setw(20) << "jenis tabungan" << setw(20) << "saldo awal" << setw(20) << "saldo akhir" << endl;
for(int i = 0; i < n ; i++)
{
cout <<setprecision(10)<< i+1 << setw(20) << orang[i].namaNasabah << setw(20) << orang[i].jenisTabungan << setw(20) << orang[i].saldoAwal << setw(20) << orang[i].saldoBaru << endl;
}
return 0;
}
void bungaTahunan(Orang orang[100])
{
if(orang[i].jenisTabungan == 1)
{
saldoMin = 10000000;
bunga = 0.01;
biayaAdministrasi = 10000;
}
else if (orang[i].jenisTabungan == 2)
{
saldoMin = 50000000;
bunga = 0.02;
biayaAdministrasi = 25000;
}
orang[i].saldoBaru = 0;
for(int i = 0; i<12; i++)
{
if(orang[i].saldoAwal < saldoMin)
{
orang[i].saldoBaru += orang[i].saldoAwal + (orang[i].saldoAwal * bunga) - biayaAdministrasi;
}
else
{
orang[i].saldoBaru += orang[i].saldoAwal + (orang[i].saldoAwal * bunga);
}
}
}
Sorry if its in Indonesian Language.
Last lines of your code is:
orang[i].saldoBaru = 0;
...
orang[i].saldoBaru = orang[i].saldoBaru + (orang[i].saldoBaru * orang[i].bunga);
saldoBaru is always 0, because "0 + 0*x" is 0
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 1 year ago.
Improve this question
I need my code to output the common numbers from two arrays but I can't seem to find the issues in my code. Here are some more words so I can fill the quota for stack's post requirement.
Here's the code.
#include <iostream>
using namespace std;
int main()
{
//Variables
int list1[5], list2[5];
cout << "Enter numbers for List No.1" << endl;
for (int i = 1; i <= 5; i++) {
cout << "Enter number " << i << ":";
cin >> list1[i];
}
cout << "Enter numbers for List No.2" << endl;
for (int i = 1; i <= 5; i++) {
cout << "Enter number " << i << ":";
cin >> list2[i];
}
{
cout << "common elements" << endl;
for (int i = 0; list1; i < i++) {
for (int i = 0; list2; i < i++)
if (list1[i] == list2[i]) {
cout << " " << list1[i];
}
}
}
return 0;
}
You should use different counter for each loop and also your loop has wrong parameters. Do it like:
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (list1[i] == list2[j]){
cout<<" "<<list1[i];
}
}
}
Also for initial values start from zero index([0]) as zero index is the first element for each array:
for (int i = 0; i < 5; i++) {
cout << "Enter number " << i << ":";
cin >> list1[i];
}
Another way to do this is by using std::array. It acts very similar to a normal array but it is much better to work with.
for example if you want a char array of length of 10 you can simply write:
std::array<char, 10> My_happy_array.
First please read this to understand why you should not using namespace std. Second consider reading about Range-based for loop
Third, here is the modified version of your program which uses std::array instead of a normal array:
#include <iostream>
#include <array>
int main() {
std::array<int,5> list1;
std::array<int,5> list2;
std::cout << "Enter numbers for List No.1" << std::endl;
for (int i = 0; i < list1.size(); ++i) {
std::cout << "Enter number " << (i+1) << ":";
std::cin >> list1[i];
}
std::cout << "Enter numbers for List No.2" << std::endl;
for (int i = 0; i < list2.size(); ++i) {
std::cout << "Enter number " << (i+1) << ":";
std::cin >> list2[i];
}
for(auto i : list1){
for(auto j : list2){
if(i==j){
std::cout << " " << i;
}
}
}
return 0;
}
Here's fixed code
#include <iostream>
using namespace std;
int main()
{
//Variables
int list1[5] ,list2[5];
cout << "Enter numbers for List No.1" << endl;
for(int i = 0; i < 5; i ++)
{
cout << "Enter number " << i << ":";
cin >> list1[i];
}
cout << "Enter numbers for List No.2" << endl;
for(int i = 0; i < 5; i ++)
{
cout << "Enter number " << i << ":";
cin >> list2[i];
}
cout<<"common elements"<<endl;
for (int i = 0;i<5; i++)
{
for (int j = 0;j<5; j++)
{
if (list1[i] == list2[j])
{
cout<<" "<<list1[i];
}
}
}
return 0;
}
mistakes in code:
you used for loops wrongly whole the time.
array of 5, stores values in index (0,1,2,3,4) not (1,2,3,4,5), so always start loop from 0 and ends before max index (for this case '4' ie. use 'i<5')
while using for loop inside other for loop never use same iterable variable like you used i in both loops use i and j different for each loop,
take a look in my code..
ask me if you get any difficulties.
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 4 years ago.
Improve this question
#include <iostream>
#include<iomanip>
#include <array>
#include<string>
using namespace std;
int main()
{
const int arraysize = 13;
string n;
int counter[13];
double sum=0;
// init counter
for (int i = 0; i < 13; i++)
counter[i] = 0;
int die1;
int die2;
for (int roll1 = 0; roll1 <= 36000000; roll1++) {
die1 = 1 + rand() % 6;
die2 = 1 + rand() % 6;
counter[die1 + die2]++;
}
cout << "Rolls" << setw(13) << "Frequency" << endl;
for (int face = 2; face < arraysize; face++)
{
for (int s = (counter[face] * 100 / 36000000); s > 0; s--) {
cout << '*'; //output for graph
}
sum = ((sum + counter[face]) / 36000000) * 100;
cout << setw(7) << face << setw(13) << counter[face] << setw(15) << fixed << setprecision(1) << sum << endl;
}
system("Pause");
return 0;
}
output of code
I'm trying to get the output like this but my graph isn't coming out the way I'd like. any suggestions one how i can do it? how do I position it correctly.
and this my output of my code.
so i ended up figuring it out. i ended up placing the graph in a different part of the code and got it working.
for (int face = 2; face < arraysize; face++)
{
sum = ((sum + counter[face]) / 36000000) * 100;
cout << setw(7) << face << setw(13) << counter[face] << setw(15) << fixed << setprecision(1) << sum;
for (int s = (counter[face] * 100 / 36000000); s > 0; s--) {
cout << '*';
}
cout << endl;
}
system("Pause");
return 0;
}
Thanks for the suggestion!
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 8 years ago.
Improve this question
I've been looking over my code for awhile now, and I can't figure out why this error keeps coming up. It's probably something really simple, but I just can't figure it out...
"Unhandled exception at 0x00FB59E6 in program.exe: 0xC0000005: Access violation writing location 0x00000009."
It happens on the third time of repeating the program
main.cpp
#include <iostream>
#include <string>
#include "functions.h"
using namespace std;
int main(){
RandomArray();
MinMaxArray();
SortedArrays();
cout << "\n\nWould you like to re-run? (Y/N): ";
cin >> y;
if (y == "Y" || y == "y"){
system("CLS");
main();
}else{
system("PAUSE");
}
return 0;
}
functions.h
#include <iostream>
#include <time.h>
using namespace std;
int array[50], used[50], sortedArray[50];
int buildSort = 1, genNum, mx = 0, mn = 100;
bool x;
string y;
int RandomArray(){
srand(time(0));
for(int a = 0; a < 50; a++){ //array generator
do{
genNum = (1+rand()%100); //generate a # between 1-100
x = false;
for(int b = 0; b < 50; b++){
if(genNum == used[b]){ //if the number is already used...
x = true;
}
}
}while(x == true);
used[a] = genNum;
array[a] = genNum;
}
cout << "Random array: " << endl;
for(int c = 0; c < 50; c++){
cout << array[c] << " "; //display the random array
}
cout << endl << endl;
return 0;
}
int MinMaxArray(){
for(int d = 0; d < 50; d++){ //for each number in the array
if(array[d] > mx){ //check to see if each number is greater than mx
mx = array[d]; //the max equals that number it picked out
}
if(array[d] < mn){ //check to see if theres a number is less than mn
mn = array[d]; //the minimum equals that number it picked out
}
}
cout << "Maximum: " << mx << endl; //display the max
cout << "Minimum: " << mn << endl << endl; //display the min
return 0;
}
int SortedArrays(){
sortedArray[0] = mn;
for(int e = mn + 1; e <= mx; e++){ //goes through 1-100 and adds each number to another array in order
for(int f = 0; f < 50; f++){
if(array[f] == e){
sortedArray[buildSort] = array[f];
buildSort++;
}
}
}
cout << "Sorted array: " << endl;
for(int g = 0; g < 50; g++){
cout << sortedArray[g] << " "; //display sorted from lowest to highest
}
cout << endl << endl;
cout << "Reverse sorted: " << endl;
for(int h = 49; h >= 0; h--){
cout << sortedArray[h] << " "; //display sorted from highest to lowest
}
return 0;
}
You are using some variables that you are initializing just the 1st time: buildSort, mx and mn;
Add at the beginning of your main something like
int main()
{
buildSort = 1;
mx = 0;
mn = 100;
RandomArray();
MinMaxArray();
SortedArrays();
}
And try again.
Your buildSort makes you run out of the array.
Just a suggestion: try to write your code better! Is almost unreadable!!!
Don't use global variable (at least not like this!)
There is not need to use system function for basic features like CLS and PAUSE, you may use clrscr() or getch() (or appropriate functions)
Don't recurse main - it it not meant for this purpose. Though no compiler will complain, it is bad approach.