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!
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 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 am a beginner and I keep getting an error when I am calling my function. I am trying to get the user to input a value for the array and then get it to display it in a table format. Also, I am trying to display a 3x3 matrix in the end.
void DMMatrix(int DM[2][2]){
int number;
cout << "Input 0's and 1's to the DM[2][2]\n\n";
for (int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
cout << "DM[" << i << "]"<< "[" << j << "]: ";
cin >> number;
DM[i][j] = number;
}
}
}
int main(){
int i, j, sum = 0;
int DM[2][2];
DMMatrix(DM[2][2]);
cout << "\n\nOutput (table format)\n\n";
for(i = 0;i < 3;i++){
for(j = 0;j < 3;j++){
cout << " " << DM[i][j] << " ";
}
cout << "\n";
}
sum = DM[0][0] + DM[0][1] + DM[0][2] + DM[1][0] + DM[1][1] + DM[1][2] + DM[2][0] + DM[2][1] + DM[2][2];
if (sum % 2 == 0){
cout << "\nSum is " << sum;
}
}
There are 3 problems in your program.
Mistake 1
You are calling the function DMMatrix incorrectly. The correct way to call DMMatrix would be:
DMMatrix(DM);//CORRECT WAY OF CALLING DMMatrix
Mistake 2
Your 2D array DM has 2 rows and 2 columns and you're accessing 3rd row and 3rd column of the array DM. But since the 3rd row and 3rd column doesn't exist, you have undefined behavior in your program. To solve this you can create a 3x3 2D array as shown below:
#include <iostream>
using namespace std;
void DMMatrix(int DM[3][3]){//2 CHANGED TO 3
int number;
cout << "Input 0's and 1's to the DM[2][2]\n\n";
for (int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
cout << "DM[" << i << "]"<< "[" << j << "]: ";
cin >> number;
DM[i][j] = number;
}
}
}
int main(){
int i, j, sum = 0;
int DM[3][3]; //2 CHANGED TO 3
DMMatrix(DM);
cout << "\n\nOutput (table format)\n\n";
for(i = 0;i < 3;i++){
for(j = 0;j < 3;j++){
cout << " " << DM[i][j] << " ";
sum += DM[i][j];//CALCULATE SUM HERE
}
cout << "\n";
}
cout << "\nSum is " << sum;
}
Mistake 3
You don't need to calculate the sum by writing each element of the array explicitly. You can calculate the sum inside the for loop in my modified above program. Also note that in your original code, while calculating the sum you were going out of bounds which leads to undefined behavior. The above shown program corrected this.
First, the size of the array that you have given to DM is 2x2 but your loop runs for 9 times considering the loop runs for 0, 1 and 2 which will cause a stack smash or Segmentation Fault
#include <iostream>
using namespace std;
void DMMatrix (int DM[3][3])
{
int number;
cout << "Input 0's and 1's to the DM[2][2]\n\n";
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << "DM[" << i << "]" << "[" << j << "]: ";
cin >> number;
DM[i][j] = number;
}
}
}
int main ()
{
int i, j, sum = 0;
int DM[3][3];
DMMatrix (DM);
cout << "\n\nOutput (table format)\n\n";
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
cout << " " << DM[i][j] << " ";
}
cout << "\n";
}
sum = DM[0][0] + DM[0][1] + DM[0][2] + DM[1][0] + DM[1][1] + DM[1][2] +
DM[2][0] + DM[2][1] + DM[2][2];
if (sum % 2 == 0)
{
cout << "\nSum is " << sum;
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
// Number Sorting Algorithm - Trey Taylor 2014
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main() {
int numbersort [] = {200, 85, 63, 4}
cout << "The numbers scrambled are: ";
cout << numbersort[0] << ", ";
cout << numbersort[1] << ", ";
cout << numbersort[2] << ", ";
cout << numbersort[3] << ", " << endl;
firstlast:
if (numbersort[0] > numbersort[1]) {
int temp = 0;
temp = numbersort[0];
numbersort[0] = numbersort[1];
numbersort[1] = temp;
}
if (numbersort[1] > numbersort[2]) {
int temp = 0;
temp = numbersort[1];
numbersort[1] = numbersort[2];
numbersort[2] = temp;
}
if (numbersort[2] > numbersort[3]) {
int temp = 0;
temp = numbersort[2];
numbersort[2] = numbersort [3];
numbersort[3] = temp;
}
while (numbersort[0] > numbersort[1]) {
goto firstlast;
}
cout << "The numbers unscrambled are: ";
cout << numbersort[0] << ", ";
cout << numbersort[1] << ", ";
cout << numbersort[2] << ", ";
cout << numbersort[3] << ", ";
}
Does anybody know if there is a way to use a for or while loop to rearrange the numbers in the array into ascending order from left to right rather than just using 3 if statements
You could use std::sort or bubble sort algorithm(link how it works). Algorithm checks is current number in loop bigger from next one. If it is change those numbers in array. At the end you get the biggest number on the end of array and smallest at beggining of array.
int main()
{
int numbersort[] = { 200, 85, 63, 4 };
int temp = 0;
// This loop sorts all numbers in the array
for (int z = 0; z < 4; ++z)
{
// This loop sorts only one number to the end
for (int i = 0; i < 3; ++i)
{
if (numbersort[i] > numbersort[i + 1])
{
temp = numbersort[i];
numbersort[i] = numbersort[i + 1];
numbersort[i + 1] = temp;
}
}
}
cout << "Sorted numbers" << endl;
for (int i = 0; i < 4; ++i)
{
cout << numbersort[i] << endl;
}
system("pause");
return 0;
}
Something like this:
i = 0;
while (i < (sizeof(numbersort)/sizeof(numbersort[0])) - 1)
{
if (numbersort[i] >= numbersort[i + 1])
{
std::swap(numbersort[i], numbersort[i + 1]);
i = 0;
}
else
{
++i;
}
}
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.
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;