Hello, I am trying to write a program that reads in a file, and uses a 2d array to store the highest and lowest temp from each month. It also outputs the average high and the average low. The file that i am reading in is called homework2.txt and looks like this:
-4 25
6 33
21 50
33 67
50 72
65 80
72 95
65 101
58 82
35 62
34 59
-6 31
the first column being the lows and the second being the highs.
I also need 4 functions: getData(reads in the file and assigns to 2d array), averageHigh(self explanatory), averageLow(self explanatory), indexHighTemp(highest temp), and indexLowTemp(lowest temp).
I wrote a lot of the program already minus the indexHighTemp and indexLowTemp because i can't seem to figure out where to even start to find them.
Some problems i am having is that the answers for my averageHigh and averageLow are both 0. I dont know if its because i assigned the file to the 2d array wrong or if i just did the two functions wrong.
any help is greatly appreciated. Thank you.
6 2288
Trev17-
I see a few problems with this - your if (input != 1) statements aren't put in a manner that you will be able to do anything except for the first. If the first fails, 0 is returned, which means the program terminates normally. I think you want more of a menu to call your functions, have you considered a while loop to read in a line, and a switch statement to call the proper function?
Have you printed out the values in the function, to make sure there is data there?
Have you printed out the values in the function, to make sure there is data there?
I have both functions (averageHigh, averageLow) being called in the program and the output for both is 0. If thats what you meant?
for the if (!File) how would you put that in so that it does something?
I have both functions (averageHigh, averageLow) being called in the program and the output for both is 0. If thats what you meant?
for the if (!File) how would you put that in so that it does something?
Ok, after looking at it a bit more, I'm pretty sure you have no data in your arrays. (I was talking about printin out the full arrays - a 'cout << temp[i][j]' type of action), but take a look at the for loop you have where you assign temperatures to the array.
You open the file, make sure it's open, but then look at what you declare in the comment as "array initialization". You set everything to 0. Your functions are probably working right, because there's no info in them.
You have File declared, you just need to read the elements in from file into the proper place. I'll give you a hint, you'll need to wrap the for loops in a while loop, and after that, instead fo assigning a place in the array 0, you need to assign it the next value from the file.
Does that help?
You have File declared, you just need to read the elements in from file into the proper place. I'll give you a hint, you'll need to wrap the for loops in a while loop, and after that, instead fo assigning a place in the array 0, you need to assign it the next value from the file.
Does that help?
I changed it to this but now it sort of freezes after it calls the getData function.
while (File)
{
for (int row = 0; row < 12; row++)
for (int col = 0; col < 2; col++)
temps[row][col]++;
}
I think this is what you meant but not sure.
I changed it to this but now it sort of freezes after it calls the getData function.
while (File)
{
for (int row = 0; row < 12; row++)
for (int col = 0; col < 2; col++)
temps[row][col]++;
}
I think this is what you meant but not sure.
Ok, so you want to pull temperatures from File, correct? So I would change the condition in your while to be (File != 'EOF') - the EOF character stands for 'End of File' and will let the while loop know that is has reached the end. Right now it's freezing because the file exists, so it's open, so it always returns true (there is no condition in which File will return false, unless you delete/move it yourself, which isn't what you want).
Second, code tags ( [ code] and [ /code] without the spaces inside) help a great deal with readability and spacing.
Third, you have
temps[row][col]++;
What do you want this to do? The ++ postincrement operator is working on the values of each, but you're not saving those values, and that's not what you really want to do anyway. Right here is where you want to read in a value from File.
Check out this tutorial from cplusplus.com, it will help with File IO.
For the high, low functions: just assign a variable to one of the temps and check to see if any of the other temps are larger (or smaller) than it. Like this: -
variable = first temp
-
if variable < second temp
-
variable = second temp
-
if variable < third temp
-
variable = third temp
-
and so on and so on
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: john |
last post by:
Relatively new to C coding, so any help would greatly be appreciated.
I'm having problems try to return my string array from my parsing
function. When I do a printf I am getting the correct value...
|
by: Prashanth Badabagni |
last post by:
#include<string.h>
void (*foobar)();
void main()
{
char *str;
strcpy(str,"Fun1");
strcat(str,"method1");
|
by: shachar |
last post by:
hi all.
i'm looking for a simple example of how to pass
optionaly, an array to a function.
if the array isn't there (it's optional) i want to know
about it.
thanks.
|
by: Andrew Gentile |
last post by:
Hello,
I have been working on a program where I need to have a function
return an array. I found out that C doesn't do this, so now I am
trying to get the function to return a pointer to an...
|
by: Christian Maier |
last post by:
Hi
After surfing a while I have still trouble with this array thing. I
have the following function and recive a Segmentation fault, how must
I code this right??
Thanks
Christian Maier
|
by: lawpoop |
last post by:
Hello!
I am working on a map of a rather large php project that I've been
working on. I hope to create a map of the files of the project that
would look like the output of the unix 'tree'...
|
by: drktmplr11 |
last post by:
Hi, this is my first post here at the forums, and I am looking for assistance with what looks to be a syntax error within my code. I am attending FIU, and looking to broaden my understanding of...
|
by: Immortal Nephi |
last post by:
I would like to design an object using class. How can this class
contain 10 member functions. Put 10 member functions into member
function pointer array.
One member function uses switch to call...
|
by: ctj951 |
last post by:
I have a very specific question about a language issue that I was
hoping to get an answer to. If you allocate a structure that
contains
an array as a local variable inside a function and return...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |