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.
any help is greatly appreciated. Thank you.
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?
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?
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?
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.
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.
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 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
|
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 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...
|
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' command:
..
|-- index.php
| |-- menu.php
| |-- content.php
|
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...
|
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....
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |