Monday, May 24, 2010

Computer science C++ programing?

#include %26lt;iostream.h%26gt;


#include %26lt;stdlib.h%26gt;


using namespace std;


int getstockprices;


float printAverage;


float printLowest;


float printhighest;


int main()


{


int stockprice;


float average;


float lowest;


float highest;





void get_stock_price(float %26amp;stockprice)


cout%26lt;%26lt;"Please enter the closing stock price for day 1."%26lt;%26lt; endl;


cin%26gt;%26gt; stockprice;


int day;


for (day = 0; day %26lt; 31; day++ )





int main()





float stockprice;


get_stock_price(stockprice);


cout %26lt;%26lt; "1st price: " %26lt;%26lt; stockprice %26lt;%26lt; endl;





system("PAUSE");


return 0;


}


my program is not working i don't know what i am doing wrong..


i have to write a program with style and function to read 31days worth of stock prices from a user.after accepting 31days worth of stock prices the program will outpout.


the highest price and the day of the month that price was reached


the lowest price and the day of the month that price was reached


the average price for the month...


i did but when i run it only stops on day 1

Computer science C++ programing?
You didn't paste your code correctly. This is a bit of a mess and I know it's not the code you are compiling and running.





From the behavior you describe, I would say check your for loop. Make sure your brackets are correct and that you are waiting until you finish your for loop to pause and return.





for (day = 0; day %26lt; 31, day++)


{


float stockprice;


get_stock_price(stockprice);


//TODO*


}


//now print out your highest price, pause and return.





The TODO is that you have to write some code that will analyze which was the highest stock price so that you will be able to print the highest price after the for loop is through. It could be as simple as keeping track of whether each entered price is the highest one so far and saving it to a highestPrice variable if it is.
Reply:I'm surprised the code above compiles. It shouldn't unless I have completely forgotten how C does the precompile pass. It should have found several syntax errors. Either that, or you left out several bits of code.





Either way, you have the concept generally right, although some logic needs to change. For example, get_stock_price() should probably accept the day number to ask for. Running averages and actual averages differ, slightly so it may be best to create an array for all the values.





I would initialize lowest and highest to the first day's data and adjust during each iteration. I also don't see a reason to use a reference to return the stock price input by the user. Use the function's return value instead; that's what it's for.
Reply:google cprogramming site and read your code, don't be DAMN LAZY ***


No comments:

Post a Comment