Note for simplicity we are assuming scanf succeeds in reading an integer.
A robust program would check that scanf returns 1 to indicate an integer read.
The constants 4 & 5 below would be better replaced with a #define
#include <stdio.h>
int main(void) {
int x[5], i, j;
printf("Enter 5 numbers: ");
i = 0;
while (i < 5) {
scanf("%d", &x[i]);
i = i + 1;
}
printf("Numbers reversed are:\n");
j = 4;
while (j >= 0) {
printf("%d\n", x[j]);
j = j - 1;
}
return 0;
}
Note for simplicity we are assuming scanf succeeds in reading an integer.
A robust program would check that scanf returns 1 to indicate an integer read.
#include <stdio.h>
#define MAX_NUMBERS 1000000
int main(void) {
int x[MAX_NUMBERS], i, j, howMany;
printf("Read how many numbers? ");
scanf("%d", &howMany);
if (howMany > MAX_NUMBERS) {
printf("I'm sorry, Dave. I'm afraid I can't do that.\n");
printf("Reading %d numbers\n", MAX_NUMBERS);
howMany = MAX_NUMBERS;
}
i = 0;
while (i < howMany) {
scanf("%d", &x[i]);
i = i + 1;
}
printf("Numbers reversed are:\n");
j = howMany - 1;
while (j >= 0) {
printf("%d\n", x[j]);
j = j - 1;
}
return 0;
}
A simple program which reads integers and prints snap
and exits if the same number is read twice
This version uses a sentinel variable (stopNow)
The use of a sentinel variable is very useful programming pattern
which can be used in many situations
But see variant_snap1.c for a simpler solution using return
#include <stdio.h>
#define MAX_NUMBERS 100000
int main(void) {
int numbers[MAX_NUMBERS];
int nNumbersRead, i, stopNow;
nNumbersRead = 0;
stopNow = 0;
while (stopNow == 0) {
printf("Enter a number: ");
if (scanf("%d", &numbers[nNumbersRead]) != 1) {
stopNow = 1;
} else {
i = 0;
while (i < nNumbersRead) {
if (numbers[i] == numbers[nNumbersRead]) {
printf("Snap!\n");
stopNow = 1;
}
i = i + 1;
}
nNumbersRead = nNumbersRead + 1;
if (nNumbersRead == MAX_NUMBERS) {
printf("Sorry my array is full I have to stop!\n");
stopNow = 1;
}
}
}
return 0;
}
A simple program which reads integers and prints snap
and exits if the same number is read twice
Note the use of return to leave the main function
and hence finish program execution
#include <stdio.h>
#define MAX_NUMBERS 100000
int main(void) {
int numbers[MAX_NUMBERS];
int nNumbersRead, i;
nNumbersRead = 0;
while (nNumbersRead < MAX_NUMBERS) {
printf("Enter a number: ");
if (scanf("%d", &numbers[nNumbersRead]) != 1) {
return 0;
}
i = 0;
while (i < nNumbersRead) {
if (numbers[i] == numbers[nNumbersRead]) {
printf("Snap!\n");
return 0;
}
i = i + 1;
}
nNumbersRead = nNumbersRead + 1;
}
printf("Sorry my array is full I have to stop!\n");
return 0;
}
Read annual rainfall and plot as bar graph
This version always reads N_YEARS of rainfall
Sample execution:
% dcc plot_rainfall0.c
% a.out
Enter 10 years of rainfall totals
Enter year: 2005
Enter rainfall(mm): 816
Enter year: 2006
Enter rainfall(mm): 994.0
Enter year: 2007
Enter rainfall(mm): 1499.2
Enter year: 2008
Enter rainfall(mm): 1082.6
Enter year: 2009
Enter rainfall(mm): 956.2
Enter year: 2010
Enter rainfall(mm): 1153.8
Enter year: 2011
Enter rainfall(mm): 1369.2
Enter year: 2012
Enter rainfall(mm): 1213.6
Enter year: 2013
Enter rainfall(mm): 1344.4
Enter year: 2014
Enter rainfall(mm): 893.8
1 asterisk == 100 mm of rainfall
2005 ********
2006 *********
2007 **************
2008 **********
2009 *********
2010 ***********
2011 *************
2012 ************
2013 *************
2014 ********
#include <stdio.h>
#define N_YEARS 10
#define SCALE 100
int main(void) {
int whichYear[N_YEARS];
double rainfall[N_YEARS];
int year, asterisk, nAsterisks;
printf("Enter %d years of rainfall totals\n", N_YEARS);
year = 0;
while (year < N_YEARS) {
printf("Enter year: ");
scanf("%d", &whichYear[year]);
printf("Enter rainfall(mm): ");
scanf("%lf", &rainfall[year]);
year = year + 1;
}
printf("\n1 asterisk == %d mm of rainfall\n", SCALE);
year = 0;
while (year < N_YEARS) {
printf("%4d ", whichYear[year]);
nAsterisks = rainfall[year] / SCALE;
asterisk = 0;
while (asterisk < nAsterisks) {
printf("*");
asterisk = asterisk + 1;
}
printf("\n");
year = year + 1;
}
return 0;
}
Read annual rainfall and plot as bar graph
This version asks the user how many years of rainfall they wish to plot
Sample execution:
% dcc plot_rainfall1.c
% a.out
How many years of rainfall do you want to graph: 10
Enter year: 2005
Enter rainfall(mm): 816
Enter year: 2006
Enter rainfall(mm): 994.0
Enter year: 2007
Enter rainfall(mm): 1499.2
Enter year: 2008
Enter rainfall(mm): 1082.6
Enter year: 2009
Enter rainfall(mm): 956.2
Enter year: 2010
Enter rainfall(mm): 1153.8
Enter year: 2011
Enter rainfall(mm): 1369.2
Enter year: 2012
Enter rainfall(mm): 1213.6
Enter year: 2013
Enter rainfall(mm): 1344.4
Enter year: 2014
Enter rainfall(mm): 893.8
1 asterisk == 100 mm of rainfall
2005 ********
2006 *********
2007 **************
2008 **********
2009 *********
2010 ***********
2011 *************
2012 ************
2013 *************
2014 ********
#include <stdio.h>
#define MAXIMUM_YEARS 20000
#define SCALE 100
int main(void) {
int whichYear[MAXIMUM_YEARS];
double rainfall[MAXIMUM_YEARS];
int year, asterisk, nAsterisks, nYears;
printf("How many years of rainfall do you want to graph: ");
scanf("%d", &nYears);
if (nYears > MAXIMUM_YEARS) {
printf("Limiting years read to maximum possible: %d\n", MAXIMUM_YEARS);
nYears = MAXIMUM_YEARS;
}
year = 0;
while (year < nYears) {
printf("Enter year: ");
scanf("%d", &whichYear[year]);
printf("Enter rainfall(mm): ");
scanf("%lf", &rainfall[year]);
year = year + 1;
}
printf("\n1 asterisk == %d mm of rainfall\n", SCALE);
year = 0;
while (year < nYears) {
printf("%4d ", whichYear[year]);
nAsterisks = rainfall[year] / SCALE;
asterisk = 0;
while (asterisk < nAsterisks) {
printf("*");
asterisk = asterisk + 1;
}
printf("\n");
year = year + 1;
}
return 0;
}
Read annual rainfall and plot as bar graph
This version reads years & their rainfall until the users enters 0 for a year
Sample execution:
% dcc plot_rainfall2.c
% a.out
Enter year[0 to stop]: 2005
Enter rainfall(mm): 816
Enter year[0 to stop]: 2006
Enter rainfall(mm): 994.0
Enter year[0 to stop]: 2007
Enter rainfall(mm): 1499.2
Enter year[0 to stop]: 2008
Enter rainfall(mm): 1082.6
Enter year[0 to stop]: 2009
Enter rainfall(mm): 956.2
Enter year[0 to stop]: 2010
Enter rainfall(mm): 1153.8
Enter year[0 to stop]: 2011
Enter rainfall(mm): 1369.2
Enter year[0 to stop]: 2012
Enter rainfall(mm): 1213.6
Enter year[0 to stop]: 2013
Enter rainfall(mm): 1344.4
Enter year[0 to stop]: 2014
Enter rainfall(mm): 893.8
Enter year: 0
1 asterisk == 100 mm of rainfall
2005 ********
2006 *********
2007 **************
2008 **********
2009 *********
2010 ***********
2011 *************
2012 ************
2013 *************
2014 ********
#include <stdio.h>
#define MAXIMUM_YEARS 20000
#define SCALE 100
int main(void) {
int whichYear[MAXIMUM_YEARS];
double rainfall[MAXIMUM_YEARS];
int year, asterisk, nAsterisks, nYears;
year = 0;
nYears = MAXIMUM_YEARS;
while (year < MAXIMUM_YEARS) {
printf("Enter year[0 to stop]: ");
scanf("%d", &whichYear[year]);
if (whichYear[year] == 0) {
nYears = year;
year = MAXIMUM_YEARS;
} else {
printf("Enter rainfall(mm): ");
scanf("%lf", &rainfall[year]);
year = year + 1;
}
}
printf("\n1 asterisk == %d mm of rainfall\n", SCALE);
year = 0;
while (year < nYears) {
printf("%4d ", whichYear[year]);
nAsterisks = rainfall[year] / SCALE;
asterisk = 0;
while (asterisk < nAsterisks) {
printf("*");
asterisk = asterisk + 1;
}
printf("\n");
year = year + 1;
}
return 0;
}
Read annual rainfall and plot as bar graph
This version reads years & their rainfall until end-of-file
Sample execution:
% dcc plot_rainfall3.c
% a.out
Enter year: 2005
Enter rainfall(mm): 816
Enter year: 2006
Enter rainfall(mm): 994.0
Enter year: 2007
Enter rainfall(mm): 1499.2
Enter year: 2008
Enter rainfall(mm): 1082.6
Enter year: 2009
Enter rainfall(mm): 956.2
Enter year: 2010
Enter rainfall(mm): 1153.8
Enter year: 2011
Enter rainfall(mm): 1369.2
Enter year: 2012
Enter rainfall(mm): 1213.6
Enter year: 2013
Enter rainfall(mm): 1344.4
Enter year: 2014
Enter rainfall(mm): 893.8
<control-D>
1 asterisk == 100 mm of rainfall
2005 ********
2006 *********
2007 **************
2008 **********
2009 *********
2010 ***********
2011 *************
2012 ************
2013 *************
2014 ********
#include <stdio.h>
#define MAXIMUM_YEARS 20000
#define SCALE 100
int main(void) {
int whichYear[MAXIMUM_YEARS];
double rainfall[MAXIMUM_YEARS];
int year, asterisk, nAsterisks, nYears;
year = 0;
nYears = MAXIMUM_YEARS;
while (year < MAXIMUM_YEARS) {
printf("Enter year: ");
if (scanf("%d", &whichYear[year]) == 0) {
nYears = year;
year = MAXIMUM_YEARS;
} else {
printf("Enter rainfall(mm): ");
scanf("%lf", &rainfall[year]);
year = year + 1;
}
}
printf("\n1 asterisk == %d mm of rainfall\n", SCALE);
year = 0;
while (year < nYears) {
printf("%4d ", whichYear[year]);
nAsterisks = rainfall[year] / SCALE;
asterisk = 0;
while (asterisk < nAsterisks) {
printf("*");
asterisk = asterisk + 1;
}
printf("\n");
year = year + 1;
}
return 0;
}
Read annual rainfall and plot as bar graph
This version reads years & their rainfall until end-of-file
and puts -1 in the array as a a marker
Sample execution:
% dcc plot_rainfall4.c
% a.out
Enter year: 2005
Enter rainfall(mm): 816
Enter year: 2006
Enter rainfall(mm): 994.0
Enter year: 2007
Enter rainfall(mm): 1499.2
Enter year: 2008
Enter rainfall(mm): 1082.6
Enter year: 2009
Enter rainfall(mm): 956.2
Enter year: 2010
Enter rainfall(mm): 1153.8
Enter year: 2011
Enter rainfall(mm): 1369.2
Enter year: 2012
Enter rainfall(mm): 1213.6
Enter year: 2013
Enter rainfall(mm): 1344.4
Enter year: 2014
Enter rainfall(mm): 893.8
<control-D>
1 asterisk == 100 mm of rainfall
2005 ********
2006 *********
2007 **************
2008 **********
2009 *********
2010 ***********
2011 *************
2012 ************
2013 *************
2014 ********
#include <stdio.h>
#define MAXIMUM_YEARS 20000
#define SCALE 100
int main(void) {
int whichYear[MAXIMUM_YEARS];
double rainfall[MAXIMUM_YEARS];
int year, asterisk, nAsterisks;
year = 0;
while (year < MAXIMUM_YEARS) {
printf("Enter year: ");
if (scanf("%d", &whichYear[year]) == 0) {
whichYear[year] = -1;
year = MAXIMUM_YEARS;
} else {
printf("Enter rainfall(mm): ");
scanf("%lf", &rainfall[year]);
year = year + 1;
}
}
whichYear[MAXIMUM_YEARS - 1] = -1;
printf("\n1 asterisk == %d mm of rainfall\n", SCALE);
year = 0;
while (whichYear[year] >= 0) {
printf("%4d ", whichYear[year]);
nAsterisks = rainfall[year] / SCALE;
asterisk = 0;
while (asterisk < nAsterisks) {
printf("*");
asterisk = asterisk + 1;
}
printf("\n");
year = year + 1;
}
return 0;
}
A simple program which reads integers in the range 1..99
and prints snap and exits when the same number is read twice
Note for simplicity we are assuming scanf succeeds in reading an integer.
A robust program would check that scanf returns 1 to indicate an integer read.
#include <stdio.h>
#define LARGEST_NUMBER 99
int main(void) {
int i, n, snap;
int numberCounts[LARGEST_NUMBER + 1];
i = 0;
while (i < LARGEST_NUMBER) {
numberCounts[i] = 0;
i = i + 1;
}
snap = 0;
while (snap == 0) {
printf("Enter a number: ");
scanf("%d", &n);
if (n < 0 || n > LARGEST_NUMBER) {
printf("number has to be between 0 and 99 inclusive\n");
} else {
numberCounts[n] = numberCounts[n] + 1;
if (numberCounts[n] > 1) {
printf("Snap!\n");
snap = 42;
}
}
}
return 0;
}
A simple program which reads integers in the range 1..99
and prints how many time each integer has been read.
#include <stdio.h>
#define LARGEST_INTEGER 99
int main(void) {
// the array element at index i
// contains a count of how many times integer i has been seen
int integer_counts[LARGEST_INTEGER + 1];
// initialise all array elements to zero
// this could also be done by changing the declaration to
// int integer_counts[LARGEST_INTEGER + 1] = {0};
int i = 0;
while (i < LARGEST_INTEGER) {
integer_counts[i] = 0;
i = i + 1;
}
while (1) {
int n;
printf("Enter a number: ");
if (scanf("%d", &n) != 1) {
return 0;
}
if (n < 0 || n > LARGEST_INTEGER) {
printf("number has to be between 0 and %d inclusive\n", LARGEST_INTEGER);
} else {
integer_counts[n] = integer_counts[n] + 1;
printf("You have entered %d %d times\n", n, integer_counts[n]);
}
}
}
Read 5 numbers and print them in reverse order - the hard way
This approach quickly becomes impractical if you want to read more numbers a much better approach is to use an array