Wednesday, October 26, 2011

#include
void up_and_down(int);

int main(void)
{
up_and_down(1);
return 0;
}

void up_and_down(int n)
{
printf("Level %d: n location %p\n", n, &n);
if (n < 4)
up_and_down(n+1);
printf("LEVEL %d: n location %p\n", n, &n);
}


#include
#define MONTHS 12

int main(void)
{
int days[MONTHS] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int index;
for (index = 0; index < MONTHS; index++)
printf("Month %d has %2d days.\n", index +1, days[index]);
return 0;
}



#include
#define MONTHS 12
int main(void)
{
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31};
int index;

for (index = 0; index < sizeof days / sizeof days[0]; index++)
printf("Month %2d has %d days.\n", index +1, days[index]);

return 0;
}


#include
#define MONTHS 12 //number of months in a year
#define YEARS 5 //number of years of data
int main(void)
{
//..initializing rainfall data for 2000-2004
const float rain[YEARS][MONTHS] =
{
{4.3, 4.3, 4.3, 3.0, 2.0, 1.2, 0.2, 0.2, 0.4, 2.4, 3.5, 6.6},
{8.5, 8.2, 1.2, 1.6, 2.4, 0.0, 5.2, 0.9, 0.3, 0.9, 1.4, 7.3},
{9.1, 8.5, 6.7, 4.3, 2.1, 0.8, 0.2, 0.2, 1.1, 2.3, 6.1, 8.4},
{7.2, 9.9, 8.4, 3.3, 1.2, 0.8, 0.3, 0.0, 0.6, 1.7, 4.3, 6.2},
{7.6, 5.6, 3.8, 2.8, 3.8, 0.2, 0.0, 0.0, 0.0, 1.3, 2.6, 5.2}
};
int year, month;

float subtot, total;

printf("YEAR RAINFALL (inches)\n");
for (year = 0, total=0; year < YEARS; year++)
{
//for each year, sum rainfall for each month
for (month = 0, subtot = 0; month < MONTHS; month++)
subtot += rain[year][month];
printf("%5d %15.1f\n", 2000 + year, subtot);
total += subtot; //total for all years
}

printf("\nThe yearly average is %.1f inches.\n\n", total/YEARS);
printf("MONTHLY AVERAGES:\n\n");
printf(" JAN FEB MAR APR MAY JUN JUL AUG SEP OCT");
printf(" NOV DEC \n");

for (month = 0; month < MONTHS; month++)
{
//for each month, sum rainfall over years
for (year = 0, subtot = 0; year < YEARS; year++)
{
subtot += rain[year][month];
}
printf("%4.1f ", subtot/YEARS);
}
printf("\n");
return 0;
}


#include
#define SIZE 4
int main(void)
{
short dates[SIZE];
short *pti;
short index;
double bills[SIZE];
double *ptf;

pti = dates;
ptf = bills;
printf("%23s %10s\n", "short", "double");
for (index = 0; index < SIZE; index++)
{
printf("pointers + %d: %10p %10p\n", index, pti + index, ptf + index);
}

return 0;

}


#include
#define MONTHS 12

int main(void)
{
int days[MONTHS] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int index;

for (index = 0; index < MONTHS; index++)
{
printf("Month %2d has %d days.\n", index +1, *(days + index)); //same as days[index]


}

return 0;
}


#include
#define SIZE 10
int sump(int * start, int * end);
int main(void)
{
int marbles[SIZE] = {20, 10, 5, 39, 4, 16, 19, 26, 31, 20};
long answer;

answer = sump(marbles, marbles + SIZE);
printf("The total number of marbles is %ld.\n", answer);

return 0;
}

//use pointer arithmetic
int sump(int * start, int * end)
{
int total = 0;

while (start < end)
{
total += *start;
start++;
}

return total;
}

No comments: