Y
yanyo
hi, im trying to figure out whats the problem with this program i get a
runtime error but i dont see where the problem is i tried changing
declaration but nothing if somrbody can try this on their compiler to make
sure is not a compiler compatibility problem.
#include<stdio.h>
#include<math.h>
char menu_action(void);
int menu_faith(void);
void showStaistics(void);
int bdonations[100];
int cdonations[100];
int jdonations[100];
int mdonations[100];
int odonations[100];
int totals[5];
int bTotal = 0;
int cTotal = 0;
int jTotal = 0;
int mTotal = 0;
int oTotal = 0;
int main(void)
{
char option,faith;
//array to keep track of donations of each religion
int lastDonation;
int religion;
while(1)
{
//show main menu
option = menu_action();
if( option == 'Q' || option == 'q'){
showStaistics();
return 0;
}
if( option == 'D' || option == 'd')
{
lastDonation = getUserDonation();
//get the religion
religion = menu_faith();
if( religion == 0 )
{
if( bTotal < 100 ){
bdonations[bTotal++] = lastDonation;
totals[0] += lastDonation;
}
}
else
if( religion == 1 )
{
if( cTotal < 100 ){
cdonations[cTotal++] = lastDonation;
totals[1] += lastDonation;
}
}
else
if( religion == 2 )
{
if( jTotal < 100 ){
jdonations[jTotal++] = lastDonation;
totals[2] += lastDonation;
}
}
else
if( religion == 3 )
{
if( mTotal < 100 ){
mdonations[mTotal++] = lastDonation;
totals[3] += lastDonation;
}
}
else
if( religion == 4 )
{
if( oTotal < 100 ){
odonations[oTotal++] = lastDonation;
totals[4] += lastDonation;
}
}
}
else
if( option == 'S' || option == 's')
{
showStaistics();
}
}
}
char menu_action(void)
{
char option;
while(1)
{
printf("\n Main Menu\n");
printf("\n ---------\n");
printf("D: New donation\n");
printf("S: Show statistics so far\n");
printf("Q: Show statistics and quit\n");
printf("\nChoose one of the following menu options :");
fflush(stdin);
scanf("%c",&option);
if( option != 'D' && option != 'd' && option != 'S' &&
option != 's' && option != 'Q' && option != 'q' )
{
printf("\nPlease enter a valid input\n");
}
else
{
break;
}
}
return option;
}
int getUserDonation(void)
{
int donation;
printf("\nPlease enter your donation: ");
fflush(stdin);
scanf("%d",&donation);
return donation;
}
int menu_faith(void)
{
char option;
while(1)
{
printf("\nPlease enter one of the following faiths:\n");
printf("B: Buddhist\n");
printf("C: Christian\n");
printf("J: Jewish\n");
printf("M: Muslim\n");
printf("Z: any other\n");
fflush(stdin);
scanf("%c",&option);
if( option == 'B' || option == 'b' )
return 0;
if( option == 'C' || option == 'c' )
return 1;
if( option == 'J' || option == 'j' )
return 2;
if( option == 'M' || option == 'm' )
return 3;
if( option == 'Z' || option == 'z' )
return 4;
else
{
printf("\nPlease enter a valid input\n");
}
}
return option;
}
void showStaistics(void)
{ float v;
int allTotal = 0;
int i=0;
for(i=0; i<5; i++){
allTotal += totals;
}
printf("\n\nTotals :\n");
printf("--------\n");
printf("All: $%5d\n", allTotal);
printf("Budhist: $%5d\n", totals[0]);
printf("Christian: $%5d\n", totals[1]);
printf("Jew: $%5d\n", totals[2]);
printf("Muslim: $%5d\n", totals[3]);
printf("Other: $%5d\n", totals[4]);
//averages and std deviations
//All
//christian
if(cTotal == 0 ){
average = 0;
sd = 0;
}
else{
average = totals[1]/cTotal;
v = 0;
for(i=0; i<cTotal; i++){
v += pow( (cdonations - average) , 2 );
}
sd = pow(v/cTotal,.5);
}
printf("Christian: $%10.2f +/-%10.2f\n", average, sd);
//Jew
if(jTotal == 0 ){
average = 0;
sd = 0;
}
else{
average = totals[2]/jTotal;
v = 0;
for(i=0; i<jTotal; i++){
v += pow( (jdonations - average) , 2 );
}
sd = pow(v/jTotal,.5);
}
printf("Jew: $%10.2f +/-%10.2f\n", average, sd);
//Muslim
if(mTotal == 0 ){
average = 0;
sd = 0;
}
else{
average = totals[3]/mTotal;
v = 0;
for(i=0; i<mTotal; i++){
v += pow( (mdonations - average) , 2 );
}
sd = pow(v/mTotal,.5);
}
printf("Muslim: $%10.2f +/-%10.2f\n", average, sd);
//budhist
if(oTotal == 0 ){
average = 0;
sd = 0;
}
else{
average = totals[4]/oTotal;
v = 0;
for(i=0; i<oTotal; i++){
v += pow( (odonations - average) , 2 );
}
sd = pow(v/oTotal,.5);
}
printf("Other: $%10.2f +/-%10.2f\n", average, sd);
}
runtime error but i dont see where the problem is i tried changing
declaration but nothing if somrbody can try this on their compiler to make
sure is not a compiler compatibility problem.
#include<stdio.h>
#include<math.h>
char menu_action(void);
int menu_faith(void);
void showStaistics(void);
int bdonations[100];
int cdonations[100];
int jdonations[100];
int mdonations[100];
int odonations[100];
int totals[5];
int bTotal = 0;
int cTotal = 0;
int jTotal = 0;
int mTotal = 0;
int oTotal = 0;
int main(void)
{
char option,faith;
//array to keep track of donations of each religion
int lastDonation;
int religion;
while(1)
{
//show main menu
option = menu_action();
if( option == 'Q' || option == 'q'){
showStaistics();
return 0;
}
if( option == 'D' || option == 'd')
{
lastDonation = getUserDonation();
//get the religion
religion = menu_faith();
if( religion == 0 )
{
if( bTotal < 100 ){
bdonations[bTotal++] = lastDonation;
totals[0] += lastDonation;
}
}
else
if( religion == 1 )
{
if( cTotal < 100 ){
cdonations[cTotal++] = lastDonation;
totals[1] += lastDonation;
}
}
else
if( religion == 2 )
{
if( jTotal < 100 ){
jdonations[jTotal++] = lastDonation;
totals[2] += lastDonation;
}
}
else
if( religion == 3 )
{
if( mTotal < 100 ){
mdonations[mTotal++] = lastDonation;
totals[3] += lastDonation;
}
}
else
if( religion == 4 )
{
if( oTotal < 100 ){
odonations[oTotal++] = lastDonation;
totals[4] += lastDonation;
}
}
}
else
if( option == 'S' || option == 's')
{
showStaistics();
}
}
}
char menu_action(void)
{
char option;
while(1)
{
printf("\n Main Menu\n");
printf("\n ---------\n");
printf("D: New donation\n");
printf("S: Show statistics so far\n");
printf("Q: Show statistics and quit\n");
printf("\nChoose one of the following menu options :");
fflush(stdin);
scanf("%c",&option);
if( option != 'D' && option != 'd' && option != 'S' &&
option != 's' && option != 'Q' && option != 'q' )
{
printf("\nPlease enter a valid input\n");
}
else
{
break;
}
}
return option;
}
int getUserDonation(void)
{
int donation;
printf("\nPlease enter your donation: ");
fflush(stdin);
scanf("%d",&donation);
return donation;
}
int menu_faith(void)
{
char option;
while(1)
{
printf("\nPlease enter one of the following faiths:\n");
printf("B: Buddhist\n");
printf("C: Christian\n");
printf("J: Jewish\n");
printf("M: Muslim\n");
printf("Z: any other\n");
fflush(stdin);
scanf("%c",&option);
if( option == 'B' || option == 'b' )
return 0;
if( option == 'C' || option == 'c' )
return 1;
if( option == 'J' || option == 'j' )
return 2;
if( option == 'M' || option == 'm' )
return 3;
if( option == 'Z' || option == 'z' )
return 4;
else
{
printf("\nPlease enter a valid input\n");
}
}
return option;
}
void showStaistics(void)
{ float v;
int allTotal = 0;
int i=0;
for(i=0; i<5; i++){
allTotal += totals;
}
printf("\n\nTotals :\n");
printf("--------\n");
printf("All: $%5d\n", allTotal);
printf("Budhist: $%5d\n", totals[0]);
printf("Christian: $%5d\n", totals[1]);
printf("Jew: $%5d\n", totals[2]);
printf("Muslim: $%5d\n", totals[3]);
printf("Other: $%5d\n", totals[4]);
//averages and std deviations
//All
//christian
if(cTotal == 0 ){
average = 0;
sd = 0;
}
else{
average = totals[1]/cTotal;
v = 0;
for(i=0; i<cTotal; i++){
v += pow( (cdonations - average) , 2 );
}
sd = pow(v/cTotal,.5);
}
printf("Christian: $%10.2f +/-%10.2f\n", average, sd);
//Jew
if(jTotal == 0 ){
average = 0;
sd = 0;
}
else{
average = totals[2]/jTotal;
v = 0;
for(i=0; i<jTotal; i++){
v += pow( (jdonations - average) , 2 );
}
sd = pow(v/jTotal,.5);
}
printf("Jew: $%10.2f +/-%10.2f\n", average, sd);
//Muslim
if(mTotal == 0 ){
average = 0;
sd = 0;
}
else{
average = totals[3]/mTotal;
v = 0;
for(i=0; i<mTotal; i++){
v += pow( (mdonations - average) , 2 );
}
sd = pow(v/mTotal,.5);
}
printf("Muslim: $%10.2f +/-%10.2f\n", average, sd);
//budhist
if(oTotal == 0 ){
average = 0;
sd = 0;
}
else{
average = totals[4]/oTotal;
v = 0;
for(i=0; i<oTotal; i++){
v += pow( (odonations - average) , 2 );
}
sd = pow(v/oTotal,.5);
}
printf("Other: $%10.2f +/-%10.2f\n", average, sd);
}