I'm supposed to use an array to print out the highest funds raised, lowest funds raised, and also the average of the funds raised, but I have no idea how to do that. This is the code that a tutor told me to write, but it doesn't work. for(k=0;k<counter;k++)
if(biggest<funds_raised[i])
biggest=funds_raised[i];
for(i=0;i<counter;i++)
if(biggest>funds_raised[i])
biggest=funds_raised[i]; What is wrong with these lines of codes? BTW I know I'm missing the average of the funds raised, so you don't need to point that out to me.
/****************************************************************************************
* Arturo Ibarra March 16, 2009 fundraise.cpp *
* This program computes and prints the amount of fund raised by that student and keeps *
* a running total of cards sold for each company, a running total of the funds raised *
* from sales for type of card, running total of overall number of cards sold, running *
* total of overall number of funds raised. It also displays the amounts raised as an *
* array. *
* Input: Choice of cards, student id number, decision of to start or end program *
* Output: The amount of funds raised by the student, and running totals *
***********************************History***********************************************
* Who Date Description *
*****************************************************************************************
* AEI 4/16/2009 Created Program *
****************************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int stu_id[NUM_STUDENT], /*array variable for student id number*/
i, /*for the array counter after the big while*/
decision_yesno, /*variable for 1=yes, 0=no*/
counter=0, /*counter*/
total_sbcards_sold=0,/*total starbucks cards sold*/
total_jjcards_sold=0,/*total jamba juice cards sold*/
total_cscards_sold=0,/*total cold stone cards sold*/
total_cards_sold,/*total of all the cards sold*/
cards_sold;/*number of cards sold*/
double biggest=0,/*variable for the problem that I'm having*/
funds_raised [NUM_STUDENT],/*array variable for funds raised*/
total_fundrsbc=0,/*total starbucks funds raised*/
total_fundrcs=0,/*total cold stone funds raised*/
total_fundrjj=0,/*total jamba juice funds raised*/
fundrSBC_student,/*starbucks funds raised by a student*/
fundrJJC_student,/*jamba juice funds raised by a student*/
fundrCSC_student,/*cold stone funds raised by a student*/
total_fundr;/*total funds raised*/
char choice[NUM_STUDENT];/*variable array for the choice of card*/
cout<<"**************Arturo's Fund Raiser Calculator******************";
cout<<fixed<<setprecision(2);
cout<<"\n\n";
cout<<"\nDo you have data to enter regarding student sales? (1=yes, 0=no) ";
cin>>decision_yesno; //validate decision_yesno
while (decision_yesno!=1 && decision_yesno!=0)
{
cout<<"\nPlease enter a valid decision. 1=yes, 0=no ";
cin>>decision_yesno;//validate decision_yesno
}
while (decision_yesno==1)
{
cout<<"\nPlease enter the Student ID: ";
cin>>stu_id[counter]; //validate stu_id
while(!(stu_id[counter]>=1000&&stu_id[counter]<=9999))
{
cout<<"\nInvalid...please enter a valid student id number ";
cin>>stu_id[counter]; //validate stu_id
}
cout<<"\nPlease enter the type of gift card ";
cout<<"\n(S or s=Starbucks, J or j=Jamba Juice, C or c=Cold Stone): ";
cin>>choice[counter];//validate choice
while (choice[counter]!='S'&& choice[counter]!='s'&&choice[counter]!='J'&&choice[counter]!='j'&&choice[counter]!='C'&&choice[counter]!='c')
{
cout<<"\nInvalid option. Please enter a correct card option: ";
cin>>choice[counter]; //validate choice
}
cout<<"\nPlease enter the number of cards sold: ";
cin>>cards_sold[counter];//validate cards_sold
while (!(cards_sold>=0))
{
cout<<"\nPlease enter a valid number of cards sold: ";
cin>>cards_sold[counter];
}
switch(choice[counter])
{
case 'S':
case 's':
//fundrSBC_student=cards_sold * 10 * STARBUCKS
fundrSBC_student=cards_sold[counter] * 10 * STARBUCKS;
total_fundrsbc+=fundrSBC_student;
funds_raised[counter]=fundrSBC_student;
total_sbcards_sold+=cards_sold[counter];
cout<<"\nThe funds raised by student"<<stu_id[counter]<<" is: "<<fundrSBC_student;
cout<<"\nDo you have any more data to enter? (1=yes, 0=no) ";
cin>>decision_yesno;//decision_yesno
while (decision_yesno!=1 && decision_yesno!=0)
{
cout<<"\nPlease enter a valid decision. 1=yes, 0=no: ";
cin>>decision_yesno;
}
break;
case 'J':
case 'j':
//fundrJJC_student=cards_sold * 10 * JAMBA_JUICE
fundrJJC_student=cards_sold[counter] * 10 * JAMBA_JUICE;
total_fundrjj+=fundrJJC_student;
funds_raised[counter]=fundrJJC_student;
total_jjcards_sold+=cards_sold[counter];
cout<<"\nThe fund raised by student"<<stu_id[counter]<<" is: "<<fundrJJC_student;
cout<<"\nDo you have any more data to enter? (1=yes, 0=no) ";
cin>>decision_yesno;//validate decision_yesno
while (decision_yesno!=1 && decision_yesno!=0)
{
cout<<"\nPlease enter a valid decision. 1=yes, 0=no: ";
cin>>decision_yesno;
}
break;
case 'C':
case 'c':
//fundrCSC_student=cards_sold * 10 * COLD_STONE
fundrCSC_student=cards_sold[counter] * 10 * COLD_STONE;
total_fundrcs+=fundrCSC_student;
funds_raised[counter]=fundrCSC_student;
total_cscards_sold+=cards_sold[counter];
cout<<"\nThe fund raised by student"<<stu_id[counter]<<" is: "<<fundrCSC_student;
cout<<"\nDo you have any more data to enter? (1=yes, 0=no) ";
cin>>decision_yesno;//validate decision_yesno
while (decision_yesno!=1 && decision_yesno!=0)
{
cout<<"\nPlease enter a valid decision. 1=yes, 0=no: ";
cin>>decision_yesno;
}
break;
}//end switch
counter++;
}//end big while
for (i=0;i<counter;i++)
cout<<stu_id[i]<<" "<<choice[i]<<" "<<cards_sold[i]<<" "<<funds_raised[i]<<"\n";
cout<<"\n**************************End of Run Report*************************";
cout<<"\nTotal number of Starbucks gift cards sold: "<<setw(15)<<total_sbcards_sold;
cout<<"\nTotal number of Jamba Juice gift cards sold: "<<setw(15)<<total_jjcards_sold;
cout<<"\nTotal number of Cold Stone gift cards sold: "<<setw(15)<<total_cscards_sold;
cout<<"\nTotal number of all gift cards sold: "<<setw(15)<<total_cards_sold;
cout<<"\n\n\n";
cout<<"\nTotal amount of funds raised from Starbuck: "<<setw(15)<<total_fundrsbc;
cout<<"\nTotal amount of funds raised from Jamba Juice:"<<setw(15)<<total_fundrjj;
cout<<"\nTotal amount of funds raised from Cold Stone: "<<setw(15)<<total_fundrcs;
cout<<"\nTotal amount of all funds raised: "<<setw(15)<<total_fundr;
cout<<"\nThe smallest funds raised amount is:";
I didn't even notice that you edited your post to actually ask a question. Better to have put that in a new post. Try something like this (assuming all vars except i are floating point):
1 2 3 4 5 6 7 8 9
sum = smallest = biggest = funds_raised[0];
for( i = 1; i < counter; i++ ) {
sum += funds_raised[i];
if( funds_raised[i] > biggest )
biggest = funds_raised[i];
if( funds_raised[i] < smallest )
smallest = funds_raised[i];
}
average = sum / counter;
If you're not willing to read the article Bazzy linked to and post an actual question, then I'm not willing to read your unformatted source (read: use code tags) without any hint of what problem I'm supposed to be looking for.
In case you missed the ever so subtle link Bazzy provided:
Don't ask others to debug your broken code without giving a hint what sort of problem they should be searching for. Posting a few hundred lines of code, saying "it doesn't work", will get you ignored. Posting a dozen lines of code, saying "after line 7 I was expecting to see <x>, but <y> occurred instead" is much more likely to get you a response.
EDIT -- apparently Hammurabi is a kinder soul than me. ^^