COMP1511 17s1 Code Examples from Lectures on arrays Introduction to Programming

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

#include <stdio.h>

int main(void) {
    int x0, x1, x2, x3, x4;
    printf("Enter 5 numbers: ");
    scanf("%d", &x0);
    scanf("%d", &x1);
    scanf("%d", &x2);
    scanf("%d", &x3);
    scanf("%d", &x4);
    printf("Numbers reversed are:\n");
    printf("%d\n", x4);
    printf("%d\n", x3);
    printf("%d\n", x2);
    printf("%d\n", x1);
    printf("%d\n", x0);
    return 0;
}

Read 5 numbers and print them in reverse order

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 #deine

#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;
}

Read 5 numbers and print them in reverse order

#include <stdio.h>

#define N_NUMBERS 5

int main(void) {
    int x[N_NUMBERS], i, j;

    printf("Enter %d numbers: ", N_NUMBERS);
    i = 0;
    while (i < N_NUMBERS) {
        scanf("%d", &x[i]);
        i = i + 1;
    }
    printf("Numbers reversed are:\n");
    j = N_NUMBERS - 1;
    while (j >= 0) {
        printf("%d\n", x[j]);
        j = j - 1;
    }
    return 0;
}

Read 5 numbers and print them in reverse order

This version checks that the scanf was able to read the number

#include <stdio.h>

#define N_NUMBERS 5

int main(void) {
    int x[N_NUMBERS], i, j, scanfFailed;

    printf("Enter %d numbers: ", N_NUMBERS);
    i = 0;
    scanfFailed = 0;
    while (i < N_NUMBERS && scanfFailed == 0) {
        if (scanf("%d", &x[i]) != 1) {
            scanfFailed = 1;
        }
        i = i + 1;
    }
    if (scanfFailed == 1) {
        printf("Insuffcient numbers read\n");
    } else {
        printf("Numbers reversed are:\n");
        j = N_NUMBERS - 1;
        while (j >= 0) {
            printf("%d\n", x[j]);
            j = j - 1;
        }
    }
    return 0;
}

INTERNAL ERROR MISSING FILE: "/import/chopin/1/cs1511/public_html/17s1/lec/arrays/code/reverse5return.c"
INTERNAL ERROR MISSING FILE: "/import/chopin/1/cs1511/public_html/17s1/lec/arrays/code/reverse5return.c"
INTERNAL ERROR MISSING FILE: "/import/chopin/1/cs1511/public_html/17s1/lec/arrays/code/reverse5_n.c"
INTERNAL ERROR MISSING FILE: "/import/chopin/1/cs1511/public_html/17s1/lec/arrays/code/reverse5_n.c"

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;
}

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;
}

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;
}

Read SIZE x SIZE numbers and test if they form a magic square http://en.wikipedia.org/wiki/Magic_square

and exits if the same number is read twice

Lo Shu Square 4 9 2 3 5 7 8 1 6

Magic square of primes 17 89 71 113 59 5 47 29 101

#include <stdio.h>

#define SIZE 3

int main(void) {
    int x[SIZE][SIZE];
    int row, column;
    int magicConstant, sum;
    int notMagicSquare;

    // read potential magic square
    printf("Enter %d numbers please:\n", SIZE*SIZE);
    row = 0;
    while (row < SIZE) {
        column = 0;
        while (column < SIZE) {
            if (scanf("%d", &x[row][column]) != 1) {
                printf("Couldn't read enough numbers\n");
                return 0;
            }
            column = column + 1;
        }
        row = row + 1;
    }
    printf("Numbers are:\n");
    // print potential magic square
    row = 0;
    while (row < SIZE) {
        column = 0;
        while (column < SIZE) {
            printf("%d ", x[row][column]);
            column = column + 1;
        }
        printf("\n");
        row = row + 1;
    }

    // sum first row
    magicConstant = 0;
    row = 0;
    while (row < SIZE) {
        magicConstant = magicConstant + x[0][row];
        row = row + 1;
    }

    // check if sum of each row matches sum of first row
    notMagicSquare = 0;
    row = 1;
    while (row < SIZE) {
        sum = 0;
        column = 0;
        while (column < SIZE) {
            sum = sum + x[row][column];
            column = column + 1;
        }
        if (sum != magicConstant) {
            notMagicSquare = 1;
        }
        row = row + 1;
    }

    // check if sum of each columns matches sum of first rowc
    column = 0;
    while (column < SIZE) {
        sum = 0;
        row = 0;
        while (row < SIZE) {
            sum = sum + x[row][column];
            row = row + 1;
        }
        if (sum != magicConstant) {
            notMagicSquare = 1;
        }
        column = column + 1;
    }

    // summing diagonals left as an exercise

    if (notMagicSquare == 0) {
        printf("Is a magic square\n");
    } else {
        printf("Not a magic square\n");
    }
    return 0;
}

Read n numbers and print them in reverse order

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;
}