Tuesday, July 14, 2009

C++ program Array help please!!!??

I am supposed to write a program that asks the user for a file name. The program should read the contents of the file and display the following data:


the lowest number, the highest, the total of the numbers, and the average of the numbers.


My program will compile but it never reads the numbers from my "numbers.txt" file. It just gives me random numbers. My teacher wants us to use functions for everything, but I don't get what to do in the function readNumbers. My teacher completely left us on our own to do this. He just walked out of class so can anyone please help me with this?

C++ program Array help please!!!??
FINAaaaaaaaaaaaaLLY I solve the problem





i hope that matches your expectations :)








#include "stdafx.h"


#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


#include %26lt;iomanip%26gt;








using namespace std;





void readNumbers(ifstream %26amp;, int [], int);


int getLowest (int [], int);


int getHighest (int [], int);


int sumArray (int [], int);


double getAverage (int [], int);





const int ARRAY_SIZE = 12;








int numbers[ARRAY_SIZE]; // array as global


// to keep the alteration of function read number





int main()


{





int total;


double average;


int highest;


int lowest;





ifstream inputFile;





readNumbers(inputFile,numbers,ARRAY_SI...





lowest = getLowest(numbers, ARRAY_SIZE);





highest = getHighest(numbers, ARRAY_SIZE);





total = sumArray(numbers, ARRAY_SIZE);





average = getAverage(numbers, ARRAY_SIZE);





cout %26lt;%26lt; "The lowest number is: " %26lt;%26lt; lowest %26lt;%26lt; endl;


cout %26lt;%26lt; "The highest number is: " %26lt;%26lt; highest %26lt;%26lt; endl;


cout %26lt;%26lt; "The total of the numbers is: " %26lt;%26lt; total %26lt;%26lt; endl;


cout %26lt;%26lt; "The average of the numbers is: " %26lt;%26lt; average %26lt;%26lt; endl;











return 0;





}








//**************************


//Definition of readNumbers*


//**************************





void readNumbers(ifstream %26amp;inputFile,int numbers [], int arraySize)


{


int count;





inputFile.open("C:\\numbers.txt");





if (!inputFile) {


cerr %26lt;%26lt; "Unable to open file";


exit(1); // call system to stop


}








for (count = 0; count %26lt; ARRAY_SIZE; count++)


inputFile %26gt;%26gt; numbers[count];





inputFile.close();





cout %26lt;%26lt; "The numbers are: ";


for (count = 0; count %26lt; ARRAY_SIZE; count++)


cout %26lt;%26lt; numbers[count] %26lt;%26lt; " ";


cout %26lt;%26lt; endl;


}








//************************


//Definition of getLowest*


//************************





int getLowest(int Narray[],int size)


{


int lowest;





lowest = Narray[0];


for (int count = 1; count %26lt; size; count++)


{


if (Narray[count] %26lt; lowest)


lowest = Narray[count];


}


return lowest;


}








//*************************


//Definition of getHighest*


//*************************





int getHighest(int Narray[], int size)


{


int highest;





highest = Narray[0];


for (int count = 1; count %26lt; size; count ++)


{


if (Narray[count] %26gt; highest)


highest = Narray[count];


}


return highest;


}





//***********************


//Definition of sumArray*


//***********************








int sumArray(int Narray[], int size)


{


int total = 0;





for (int count = 0; count %26lt; size; count++)


total += Narray[count];


return total;


}





//*************************


//Definition of getAverage*


//*************************





double getAverage(int Narray[], int size)


{


double average;





int total = 0;





for (int count = 0; count %26lt; size; count++)





total += Narray[count];





average = total / ARRAY_SIZE;





return average;


}
Reply:thanks alot my pleasure usman :) Report It

Reply:First, I don't have the complier installed on my machine. But your code does look right. The only thing that I saw that might help is where does it open the file name that you enter. I see where it takes it in, but I don't see where it is used later in the code. I hope that does help.





Here is something you have


"cin %26gt;%26gt; userFile;" where it should be "cin%26gt;%26gt; inputFile;"

floral deliveries

No comments:

Post a Comment