PDA

Click to See Complete Forum and Search --> : Help w. terms


Help
Dec 18th, 2000, 11:04 PM
#include <stdio.h>



int main() {

int temper[7];
int day, sum;


printf("Enter in 7 temps:\n ");
for (day=0;day<7;++day){
printf("Enter in temperature for day %d: ", day);
scanf("%d",&temper[day]);
}

sum=0;

for(day=0;day<7;++day)
sum += temper[day];
printf("Average is %d.",sum/7);
}



Okay first off,
1. in the for loop it initializes day to 0.
Day is not an array so how does it hold all seven integers?

2.in the second for loop it sets the variable day back to zero. How does it remember the temps then?

3. the sum+= temper[day]. What exactly is this function saying +=?

Thanks in advance.
:)

Dec 18th, 2000, 11:56 PM
Day isnt the array, temper is the array, day is just a counter.

Technocrat
Dec 19th, 2000, 11:43 AM
Question 1:
for (day=0;day<7;++day)
day = 0; Well thats a no brainer
day < 7; Loop while day is less than 7
++day; Each loop, add one to day. You could write it like day = day + 1;

Question 2:
scanf("%d",&temper[day]); You are putting the value that the user inputs in to the temper array at position day. So it would be like &temper[1] = userinput; &temper[2] = userinput; This will continue till day is more than 7.

Question 3:
sum+= temper[day]; You could write this like sum = sum + temper[day]; What you are doing is adding the value in the temper array at position day to sum. += is just a shortcut to saying sum = sum + temper[day];

Hope that answers it for you

[Edited by Technocrat on 12-19-2000 at 12:46 PM]