Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Output help

jeepman950jeepman950 OkinawaMember Posts: 3

Good afternoon,
The program I am working on isn't compiling correctly. I need to have the output for two variables ie rainfall and wind. My wind speed is not showing up on the table. Any assistance would greatly be appreciated. The variables i am using are as followed
rainfall
y
1.2
2.2
3.3
2.2
10.2
12.2
2.3
0.4
0.2
1.1
2.1
0.4
1.1
2.2
3.3
2.2
10.2
12.2
2.3
0.4
0.2
1.1
2.1
0.4
1.1
2.2
3.3
2.2
10.2
12.2
2.3
0.4
0.2
1.1
2.1
0.4
1.1
2.2
3.3
2.2
10.2
12.2
2.3
0.4
0.2
1.1
2.1
0.4
1.1
2.2
3.3
2.2
10.2
12.2
2.3
0.4
0.2
1.1
2.1
0.4
windspeed
21.1
10.2
86.6
14.3
15.6
5.5
4.4
2.2
3.3
3.6
6.8
9.7
9.5
9.6
1.0
1.1
1.2
2.2
2.3
2.5
4.5
21.1
10.2
86.6
14.3
15.6
5.5
4.4
2.2
3.3
3.6
6.8
9.7
9.5
9.6
1.0
1.1
1.2
2.2
2.3
2.5
4.5
21.1
10.2
86.6
14.3
15.6
5.5
4.4
2.2
3.3
3.6
6.8
9.7
9.5
9.6
1.0
1.1
1.2
2.2

My code is as follows:

define NUMMONTHS 12

define NUMYEARS 5

include <stdio.h>

// function prototypes
void inputdata();
void printdata();

// Global variables
// These are available to all functions
float Raindata[NUMYEARS][NUMMONTHS];
float Windspeeddata[NUMYEARS][NUMMONTHS];
char years[NUMYEARS][5] = {"2011","2012","2013","2014","2015"};
char months[NUMMONTHS][12] ={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
int main ()
{
char enterData = 'y';
printf("Do you want to input Precipatation data and Windspeed data? (y for yes)\n");
scanf("%c",&enterData);
if (enterData == 'y') {
// Call Function to Input data
inputdata();

 // Call Function to display data
 printdata();

}
else {
printf("No data was input at this time\n");
}
printf("Please try the Precipitation program again. \n");
return 0;
}
// function to inputdata
void inputdata() {
/* variable definition: */
float Rain=1.0, Windspeed=1.0;
// Input Data
for (int year=0;year < NUMYEARS; year++) {
for (int month=0; month< NUMMONTHS; month++) {
printf("Enter rain for %d, %d:\n", year+1, month+1);
printf("Enter windspeed data for %d, %d: \n", year+1, month+1);
scanf("%f",&Rain);
scanf("%f",&Windspeed);
Raindata[year][month]=Rain;
Windspeeddata[year][month]=Windspeed;
}
}
}
// Function to printdata
void printdata(){
// Print data
printf ("year\t month\t rain\t Windspeed\t\n");
for (int year=0;year < NUMYEARS; year++) {
for (int month=0; month< NUMMONTHS; month++) {
printf("%s\t %s\t %5.2f\ %5.2n", years[year],months[month],Raindata[year][month],Windspeeddata[year][month]);
}
}
}

Comments

  • pseudocoderpseudocoder Member Posts: 701 ✭✭✭

    Where you can, please post the specific compiler errors... it helps folks when milling over code.

    In your printdata function, you've got a problem the format specifier on the wind speed data - you probably want %5.2f \n.

    printf("... snip ... %5.2n", ... snip ...,Windspeeddata[year][month]);

    Make sure that you initialize your data arrays to 0 before asking for input.

    When using scanf and mixing data types such as char and floats, you may need to clean out the newline '\n' or it could be stored in your data. After reading a char, you can clear out the newline using something like

    scanf("%c", &char_var); while(getchar() != '\n'){}

  • jeepman950jeepman950 OkinawaMember Posts: 3

    Thanks I'll post the compiler error here.
    There is no compiler error it just seems not to work on the output

Sign In or Register to comment.