473,732 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function/Array problems

22 New Member
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(sel f explanatory), averageLow(self explanatory), indexHighTemp(h ighest temp), and indexLowTemp(lo west 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 2305
sicarie
4,677 Recognized Expert Moderator Specialist
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 New Member
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 Recognized Expert Moderator Specialist
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 New Member
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 Recognized Expert Moderator Specialist
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 Recognized Expert Contributor
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
3326
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 for my first element but my subsequent printf's will return garbage. Can someone let me know what I am doing wrong. Thanks in advance. -jlewis
7
1815
by: Prashanth Badabagni | last post by:
#include<string.h> void (*foobar)(); void main() { char *str; strcpy(str,"Fun1"); strcat(str,"method1");
4
1920
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
1944
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 array. It seems easy, but I keep running into the same problem. The function appears to execute properly, but when I print out the array values, only the first one is correct. The other are all wrong. A snippet of code is below. Please take a...
4
2505
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
1880
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' command: .. |-- index.php | |-- menu.php | |-- content.php
4
3537
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 programming with engineering applications. The class is an introduction to C++ with engineering emphasis. The bubble_sort function was provided by the professor. The other code was authored by myself. My question refers to calling a two dimensional...
5
3651
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 10 member functions. Can switch be replaced to member function pointer array? Please provide me an example of source code to show smart pointer inside class. Thanks....
5
2680
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 that structure, is this valid? As shown in the code below I am allocating the structure in the function and then returning the structure. I know if the structure contained only simple types (int, float) this will work without problems as you...
0
8773
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9445
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9234
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9180
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6733
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2177
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.