Skip to content

Instantly share code, notes, and snippets.

@rszewczyk
Last active November 21, 2017 22:09
Show Gist options
  • Save rszewczyk/839065c297549605c63c0ecce838fd46 to your computer and use it in GitHub Desktop.
Save rszewczyk/839065c297549605c63c0ecce838fd46 to your computer and use it in GitHub Desktop.
CMSC 104 Class 21
#include <stdio.h>
int main()
{
int arraySize = 0;
while (arraySize < 1)
{
printf("Enter array size: ");
scanf("%d", &arraySize);
if (arraySize < 1)
{
printf("Array size must be at least 1\n");
}
}
int arrayOfIntegers[arraySize];
int currentIndex = 0;
int currentValue = 0;
while (currentIndex < arraySize)
{
arrayOfIntegers[currentIndex] = currentValue;
currentValue = currentValue + 2;
currentIndex = currentIndex + 1;
}
currentIndex = 0;
while (currentIndex < arraySize)
{
printf("%d\n", arrayOfIntegers[currentIndex]);
currentIndex = currentIndex + 1;
}
return 0;
}

Assignment - Class #21 11/16/2017

In this assigment you will learn about arrays in C.

As part of this assigment you will create a program in a file called arrays1.c. When you complete the assigment you will submit arrays1.c by uploading it to me on Slack. If you do not finish today, you have until the end of class (6:45pm) on Tuesday, 11/21/2017 to turn it in. Note this doesn't necessarily mean you will have time to work on it in class on 11/21. If you don't finish today, you should try and finish it before next Tuesday.

Part 1

Copy and paste the code below into a new file and save it. Give it the name arrays1.c. Upload this file to the UMBC Linux server, compile it and run it.

#include <stdio.h>

int main()
{
  // declare an array of type `int` with size `3`
  int someIntegers[3];

  // assign a value to the first element
  someIntegers[0] = 5;

  // assign a value to the second element
  someIntegers[1] = 3;

  // assign a value to the third (last) element
  someIntegers[2] = 10;

  printf("The integer at index 0 is %d\n", someIntegers[0]);
  printf("The integer at index 1 is %d\n", someIntegers[1]);
  printf("The integer at index 2 is %d\n", someIntegers[2]);

  return 0;
}

An array in C is a variable - the same way that char, int and double are variables. The difference is that rather than hold a single value of a certain type, an array can hold multiple values of a type. The type of the array is the type of value it holds. For example an array of type int can hold multiple values of type int.

Take a look at line 6 in the program:

int someIntegers[3];

This is an array declaration. All array declarations look something like this. First comes the data type - in this case int. Next is the name you are giving this array variable. Array variable names follow the same rules as regular variables names. In this example, the name is someIntegers. The extra bit with the two brackets is what distinguishes this variable as an array (rather than a normal int). The number is the size of the array - i.e. the number of values it holds, in this case 3. So, line 6 is declaring an array variables of type int called someIntegers with size 3. An array of type double that hold 6 values named sixNumbers would be declared with

double sixNumbers[6];

Arrays store their values sequentially. To use a value in an array, you have to specify the position in the array where it is (or will be) stored. This position is called an index. Array indices start a 0 and end at one less than the size of the array. So, since the array someIntegers from our program has a size of 3, the first element is at index 0 and the last element is at index 2.

Take a look at line 9 in the program. This is an example of assigning a value at a given index (in this case the first index) in an array.

someIntegers[0] = 5;

Note that we use the name of the array, someIntegers followed by two brackets surrounding the index where we want to store the value. So, in this case we're storing the value 5 at index 0 of the array someIntegers. Similarly, lines 12 & 15 assign different values to the other two positions in the array.

Take a look at line 19 of the program.

printf("The element at index 0 is %d\n", someIntegers[0]);

You can use a value in an array by using the name of the array followed by two brackets surrounding the index of the value you want to use. So, someIntegers[0] accesses the value at index 0 (the first position in the array). So, line 19 prints the value stored at index 0 in the array someIntegers.

Now, modify the program so that someIntegers can store 4 values of type int. Store the value 11 at the last index of the array and add a line to print it.

Next modify the program by declaring a new array variable of type double with size 2. Store two values of your choosing in it and print them.

Part 2

Your program should now look something like this:

#include <stdio.h>

int main()
{
  // declare an array of type `int` with size `4`
  int someIntegers[4];

  // assign a value to the first element
  someIntegers[0] = 5;

  // assign a value to the second element
  someIntegers[1] = 3;

  // assign a value to the third (last) element
  someIntegers[2] = 10;

  someIntegers[3] = 11;

  printf("The integer at index 0 is %d\n", someIntegers[0]);
  printf("The integer at index 1 is %d\n", someIntegers[1]);
  printf("The integer at index 2 is %d\n", someIntegers[2]);
  printf("The integer at index 3 is %d\n", someIntegers[3]);

  double someNumbers[2];
  someNumbers[0] = 2.3;
  someNumbers[1] = 10.2;

  printf("The number at index 0 is %lf\n", someNumbers[0]);
  printf("The number at index 1 is %lf\n", someNumbers[1]);

  return 0;
}

It it doesn't, make it so. Upload it, compile it and run it.

Array indices (the numbers between the square brackets) are just integer values. So it stands to reason that we should be able to do the things with indices that we can do with any other values.

For example. Modify line 15 so that it looks like:

someIntegers[1 + 1] = 10;

Upload, compile and run it. This demonstrates that you can use arithmetic with array indices.

Now replace lines 14 & 15 with:

int position = 2;
someIntegers[position] = 10;

Upload, compile and run it. This demonstrates that you can use variables as array indices. This is useful for processing an array with a loop.

Now replace lines 5 and 6 with:

int arraySize = 4;
int someIntegers[arraySize];

Upload, compile and run it. This demonstrates that you can also use a variable to specify the size of an array. This is useful when the size comes from user input.

Part 3

Speaking of processing arrays with loops, replace the contents of your program with:

#include <stdio.h>

int main()
{
  int arrayOfIntegers[10];

  int currentIndex = 0;
  while (currentIndex < 10)
  {
    arrayOfIntegers[currentIndex] = currentIndex;
    currentIndex = currentIndex + 1;
  }

  return 0;
}

Upload, compile and run the program (it doesn't have any output yet).

This program stores the numbers 0 - 9 in the array arrayOfIntegers. It does so by using the loop control variable to assign each position a value (in this case the value of the loop control variable).

Modify the program to do the following:

  1. Add another loop after the first loop that prints each value in the array on its own line.
  2. Ask the user for a positive integer and use that integer as the size of the array. Make sure you get a valid value from the user and reprompt them if you don't.
  3. Instead of putting the numbers 0, 1, 2, 3... in the array, fill the array with even numbers starting at 2 (2, 4, 6, 8...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment