d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Can't Figure This Out C Programming Problem
Add Reply New Topic New Poll
Member
Posts: 24,320
Joined: Jun 4 2008
Gold: 541.37
Jul 10 2014 08:29pm
assignment for my class. here is the assignment:
Project

Part 1:
H
-
Engineering (HEg) is a new company that specializes in hurricane research. Recently the National
Oceanic and Atmospheric Administration (NOAA) awarded a contract to HEg
to develop a new software
application that will do various analyses on hurricanes and tropical storms. This SW will be delivered to
the customer in phases as specified in the contract.
Background:
Hurricane season in the Atlantic Basin, which includes the Caribbean and the Gulf of Mexico, officially
runs June 1stto November 30th. Thus, NOAA needs to have a complete application by April. You became
part of the software team responsible to develop the HEg Analyzer v1.0. The first phase of this project is
to create a menu based application for the user to select an option to execute (See Instructions section).

Forv1.0, NOAA only wants the input to be entered for all the storms for two hurricane seasons for the Atlantic Basin. It’ll then print the number of hurricanes by category and the total wind speed for the
season. It’ll also display the month with the highest number of storms with the corresponding windspeed total for that month
Instructions:
1.Display the software name and the following menu

HEg v1.0
--------
0.Exit
1.Submit Hurricane Season Storms Information
2.Print Storm Analysis
Enter Selection:


2. Selection 0: Exits the program with a message:
Thanks for using HEg v1.0
3. Selection 1: User submits the storms data for a particular hurricane year as follows:
HEg v1.0
--------
1. Exit
2. Submit Hurricane Season Storms Information
3. Print Hurricane Season Analysis Enter Selection: 1
Enter the year: 2009
Enter the total number of storms in 2009: 10

Enter the month #, wind speed (mph), and min pressure (mbar) for storm #1: 2, 100, 985 ERROR: Invalid input.
Hurricane season is from June – Nov.
Wind speed for a storm is >38 mph. Try again.


Enter the month #, wind speed (mph), and min pressure (mbar) for storm #1: 6, 30, 985 ERROR: Invalid input.
Hurricane season is from June – Nov.
Wind speed for a storm is >38 mph. Try again.

Enter the month #, wind speed (mph), and min pressure (mbar) for storm #1: 6, 70, 985 Wind speed in knots: 60.82831

Enter the month #, wind speed (mph), and min pressure (mbar) for storm #2:


3.1 After entering the year, the SW asks the user to enter the total number of storms (tropical storms and hurricanes)
3.2 Then, the SW asks the user to enter the month, wind speed (mph), and the minimum pressure (mbar) for each storm. They have to be separated by a comma.
3.3 The software should automatically check the validity of the month entered and each wind speed. The hurricane season is from June through November.
3.4 The software should also check the validity of each wind speed entered based on the chart below:
[ hurricane categories w/ corresponding wind speeds]

For example, if the user enters 2 for the month of February and/or the wrong wind speed, an Error Message should be displayed as follows:
ERROR: Invalid input.
Hurricane season is from June – Nov.
Wind speed for a storm is >38 mph. Try again.
The software should continuously ask the user to enter the correct value until a valid one is entered (Hint: Use a loop).

3.5 If the wind speed input is correct, the SW should print the wind speed in knots
4 Each hurricane season is saved into a 2D-array. The first column has the number for the month, the second column has the wind speed, and the last column the min pressure.
5 It should also keep a running total of all the valid wind speeds in knots
6 After selection 1 is complete, display the menu again.

7 Selection 2: The software will do a simple analysis of the hurricane season
7.1 Categorize each storm to its corresponding storm category: The SW should have another array that contains the total number of storms for each storm category. For example, the 2006 hurricane season has a total of 10 storms, of which: 5 are tropical storms, 3 are category one, and 2 are category three hurricanes. Thus, the category array should look like this:

0 1 2 3 4 5
5 3 0 2 0 0

7.2 It then prints the number tropical storms, the number of hurricanes for each category as follows.



7.3 In Selection 3, the SW also analysis the total number of storms per month and determines what month has the highest number of storms. And it’ll display the month number and the total wind speed for that month. Hint: you may use another array

7.5 Lastly, it should display the total wind speed for 2006, as shown in 7.3

7.6 If the user didn’t enter any information for a particular hurricane season and selects #2, an error should occur (like v.1.0)
ERROR: Hurricane Season data was not entered. Select Option 1.

8 Invalid Selection number: Print an error message if the user enters any other selection number other than 0, 1, or 2.:
ERROR: Menu option not available in HEg v1.0



I cannot seem to figure this thing out. I'm using devc++ to write with. this is my code so far but it's not working for selection #2
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int selection;

/* start screen information and phrases */

while(selection>0){

printf("\n HEG v1.0\n");
printf(" --------\n");
printf("0. Exit\n");
printf("1. Submit Hurricane Season Storms Information\n");
printf("2. Print Storm Analysis\n");

printf("\nEnter Selection: ");
scanf("%d", &selection);

int year;
int r;
int storms;
int month;
int wind;
int stormarray[storms][3];
float knots;
int catarray[6]={0};
/* use switch and case command to execute code for each selection */
switch(selection)
{
case 0:
printf("\nThanks for using HEg v1.0\n\n");
break;

case 1:


printf("Enter the year: \n");
scanf("%d", &year);

printf("Enter the total number of storms in %d: ", year);
scanf("%d", &storms);
for(r=0; r<storms; r++)
{
printf("\nEnter the month #, wind speed (mph), and min pressure (mbar) for storm #%d:\n",r+1);
scanf("%d, %d, %d",&stormarray[r][0], &stormarray[r][1], &stormarray[r][2]);

if(stormarray[r][0] > 11 || stormarray[r][0] < 6)

{
printf("ERROR: Invalid input.\n Hurricane season is from June-Nov.\n Windspeed for a storm is >38mph. Try again.\n");
r--;
}

else if(stormarray[r][1] > 38)
{

/* tropical storm */
if(stormarray[r][1]>38 && stormarray[r][1]<74)
{

catarray[0]++;

knots=((stormarray[r][1])*(.8689762));
printf("Wind speed in knots:%f\n ", knots);
}
/* category 1 */
else if(stormarray[r][1]>73 && stormarray[r][1]<96)
{
catarray[1]++;

knots=((stormarray[r][1])*(.8689762));
printf("Wind speed in knots:%f\n ", knots);
}
/* category 2 */
else if(stormarray[r][1]>95 && stormarray[r][1]<111)
{
catarray[2]++;

knots=((stormarray[r][1])*(.8689762));
printf("Wind speed in knots:%f\n ", knots);
}
/* category 3 */
else if(stormarray[r][1]>110 && stormarray[r][1]<130)
{
catarray[3]++;

knots=((stormarray[r][1])*(.8689762));
printf("Wind speed in knots:%f\n ", knots);
}
/* category 4 */
else if(stormarray[r][1]>129 && stormarray[r][1]<157)
{
catarray[4]++;

knots=((stormarray[r][1])*(.8689762));
printf("Wind speed in knots:%f\n ", knots);
}
/* category 5 */
else
{
catarray[5]++;

knots=((stormarray[r][1])*(.8689762));
printf("Wind speed in knots:%f\n ", knots);
}
}
else{
printf("ERROR: Invalid input.\n Hurricane season is from June-Nov.\n Windspeed for a storm is >38mph. Try again.\n");
r--;
}

}
break;
case 2:
printf(" %d Hurricane Season Aanalysis\n", year);
printf(" ------------------------------\n");
printf("\nTotal number of tropical storms:%d \n",catarray[0]);
printf("Total number of Category 1 Hurricanes:%d \n",catarray[1]);
printf("Total number of Category 2 Hurricanes:%d \n",catarray[2]);
printf("Total number of Category 3 Hurricanes:%d \n",catarray[3]);
printf("Total number of Category 4 Hurricanes:%d\n",catarray[4]);
printf("Total number of Category 5 Hurricanes:%d \n",catarray[5]);
break;
}
}

system("PAUSE");
return 0;
}



Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jul 10 2014 09:12pm
Didn't care to read the entire project or code but one blatant error is you are defining a two dimensional array with an variable which should not compile, or at least throw an error. Also the storms variable is uninitialised so your array may be any size between 0 and like how ever many integers into is (think its like 2 million or billion or something).
Member
Posts: 24,320
Joined: Jun 4 2008
Gold: 541.37
Jul 10 2014 09:53pm
Quote (AbDuCt @ Jul 10 2014 10:12pm)
Didn't care to read the entire project or code but one blatant error is you are defining a two dimensional array with an variable which should not compile, or at least throw an error. Also the storms variable is uninitialised so your array may be any size between 0 and like how ever many integers into is (think its like 2 million or billion or something).


the storms is a variable entered by the user..it's scanned
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jul 10 2014 10:01pm
Quote (JonBully @ Jul 10 2014 11:53pm)
the storms is a variable entered by the user..it's scanned


Yes but you are trying to use it before it is initialised. Plus your compiler should be giving you an error anyways since you are not allowed to define an array with dynamic variables. Create a static size or use malloc to create a dynamically growing array.

Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jul 10 2014 10:03pm
Edit:: also change to a non shit ide and compiler. I suggest code::blocks and mingw GCC. If you have yo visual studios and its compiler is alright.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jul 10 2014 10:40pm
Quote (AbDuCt @ Jul 11 2014 12:01am)
Yes but you are trying to use it before it is initialised. Plus your compiler should be giving you an error anyways since you are not allowed to define an array with dynamic variables. Create a static size or use malloc to create a dynamically growing array.


If you still have no figured out what I was pointing at, Read your code line by line starting at `int year;` and tell me what `storms` value is when you reach `float knots`

If you answered "I don't know" then you are correct because it is uninitialized and can be any value between the min and max values of `int` unless of course your compiler auto initializes it to 0. In that case your array is now `int stormarray[0][2]`

If you can't see a problem here you should re-read your class notes or ask your teacher about uninitialized variables.
Member
Posts: 24,320
Joined: Jun 4 2008
Gold: 541.37
Jul 11 2014 10:00am
Quote (AbDuCt @ Jul 10 2014 11:40pm)
If you still have no figured out what I was pointing at, Read your code line by line starting at `int year;` and tell me what `storms` value is when you reach `float knots`

If you answered "I don't know" then you are correct because it is uninitialized and can be any value between the min and max values of `int` unless of course your compiler auto initializes it to 0. In that case your array is now `int stormarray[0][2]`

If you can't see a problem here you should re-read your class notes or ask your teacher about uninitialized variables.


i see what you mean about the array. looked back through my notes and that's the reason it's giving me garbage numbers. however im not sure how to define an array whose size is determined by the user.

also my teacher uses devc++ in class so i just use it. not a big fan of it though. it wont even let me comment lines out with "//"
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jul 11 2014 11:20am
Quote (JonBully @ Jul 11 2014 12:00pm)
i see what you mean about the array. looked back through my notes and that's the reason it's giving me garbage numbers. however im not sure how to define an array whose size is determined by the user.

also my teacher uses devc++ in class so i just use it. not a big fan of it though. it wont even let me comment lines out with "//"


Look into the functions malloc, calloc, and realloc. That will allow you to ask the user for the size and then dynamically allocate an array from a poibter via those functions.

If you need a two dimensional array you may have to create a one dimensional array but do some math to fine you other indexes.

I am on the go at the moment but if you want to use a single dimension array to emulate a two dimensional then I can show you. If you don't want to do that google " how to create two dimensional array with malloc"
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll