473,383 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Function/Array problems

22
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.

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. //function prototypes
  8. void getData();
  9. double averageHigh();
  10. double averageLow();
  11. int indexHighTemp();
  12. int indexLowTemp();
  13.  
  14. //declared arrays in file
  15. int temps[12][2];
  16.  
  17. //main part of program
  18. int _tmain(int argc, _TCHAR* argv[])
  19. {
  20.     //declared variables
  21.     int a;
  22.     int b;
  23.     int c;
  24.     int d;
  25.     int e;
  26.  
  27.     cout << "Press 1 to read the temp. file or 2 to exit" << endl;
  28.     cin >> a;
  29.  
  30.     //exit if statement
  31.     if (a != 1)
  32.         return 0;
  33.  
  34.     //calling the function
  35.     getData();
  36.  
  37.     cout << "Press 1 if you want to find the average high temp" << endl;
  38.     cin >> b;
  39.  
  40.     if (b != 1)
  41.         return 0;
  42.  
  43.     cout << "The average high is: "<< averageHigh() << endl;
  44.  
  45.     cout << "Press 1 if you want to find the average low temp" << endl;
  46.     cin >> c;
  47.  
  48.     if (c != 1)
  49.         return 0;
  50.  
  51.     cout << "The average low is: " << averageLow() << endl;
  52.  
  53.     cout << "Press 1 if you want to find the highest temp" << endl;
  54.     cin >> d;
  55.  
  56.     if (d != 1)
  57.         return 0;
  58.  
  59.     //cout << "The Hottest temperature was: " << indexHighTemp() << endl;
  60.  
  61.     cout << "Press 1 if you want to find the lowest temp" << endl;
  62.     cin >> e;
  63.  
  64.     if (e != 1)
  65.         return 0;
  66.  
  67.     //cout << "The Coldest temperature was: " << indexLowTemp() << endl;
  68.  
  69. system("pause");
  70. return 0;
  71. }
  72.  
  73. //getData function
  74. void getData()
  75. {
  76.     //declared file stream variable
  77.     ifstream File;
  78.  
  79.     //opens file
  80.     File.open("homework2.txt");
  81.  
  82.     if (!File)
  83.     {
  84.         cout << "Error cannot open file!" << endl;
  85.     }
  86.  
  87.         //array initialization
  88.         for (int row = 0; row < 12; row++)
  89.             for (int col = 0; col < 2; col++)
  90.                 temps[row][col] = 0;
  91.  
  92. }
  93.  
  94. //average high function
  95. double averageHigh()
  96. {
  97.     double avgHigh;
  98.  
  99.     //calculates the average high(the highs are the 2nd column)
  100.     avgHigh = (temps[1][2]+ temps[2][2]+temps[3][2]+temps[4][2]+temps[5][2]+temps[6][2]+temps[7][2]
  101.               +temps[8][2]+temps[9][2]+temps[10][2]+temps[11][2]+temps[12][2])/12;
  102.  
  103.     //return the average high
  104.     return avgHigh;
  105. }
  106.  
  107. //average low function
  108. double averageLow()
  109. {    
  110.     double avgLow;
  111.  
  112.     //calculates the average low(the lows are the 1st column)
  113.     avgLow = (temps[1][1]+ temps[2][1]+temps[3][1]+temps[4][1]+temps[5][1]+temps[6][1]+temps[7][1]
  114.               +temps[8][1]+temps[9][1]+temps[10][1]+temps[11][1]+temps[12][1])/12;
  115.  
  116.     //returns the average low
  117.     return avgLow;
  118. }
  119.  
  120. //highest temp function
  121. //int indexHighTemp()
  122. //{
  123.     //int Hottest;
  124.  
  125.     //calculates the hottest temperature
  126.     //Hottest =
  127.  
  128.     //return Hottest;
  129.  
  130. //}
  131.  
  132. //lowest temp function
  133. //int indexLowTemp()
  134. //{
  135.     //int Coldest;
  136.  
  137.     //calculates the coldest temperature
  138.     //Coldest = 
  139.  
  140.     //return Coldest;
  141. //}
  142.  
any help is greatly appreciated. Thank you.
Apr 23 '07 #1
6 2288
sicarie
4,677 Expert Mod 4TB
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?
Apr 23 '07 #2
Trev17
22
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?
Apr 23 '07 #3
sicarie
4,677 Expert Mod 4TB
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?
Apr 23 '07 #4
Trev17
22
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.
Apr 23 '07 #5
sicarie
4,677 Expert Mod 4TB
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.
Apr 23 '07 #6
ilikepython
844 Expert 512MB
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:
Expand|Select|Wrap|Line Numbers
  1. variable = first temp
  2. if variable < second temp
  3.     variable = second temp
  4. if variable < third temp
  5.     variable = third temp
  6. and so on and so on
  7.  
Apr 23 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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...
7
by: Prashanth Badabagni | last post by:
#include<string.h> void (*foobar)(); void main() { char *str; strcpy(str,"Fun1"); strcat(str,"method1");
4
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.
19
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...
4
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
6
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'...
4
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...
5
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...
5
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...
1
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...
0
isladogs
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...
0
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.