Skip to content

Instantly share code, notes, and snippets.

@rszewczyk
Created November 21, 2017 22:27
Show Gist options
  • Save rszewczyk/fa564711fa13dd2dafd81918b9789580 to your computer and use it in GitHub Desktop.
Save rszewczyk/fa564711fa13dd2dafd81918b9789580 to your computer and use it in GitHub Desktop.
CMSC 104 - Class 22

Assignment - Class #22 11/21/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 arrays2.c. When you complete the assigment you will submit arrays2.c by uploading it to me on Slack. If you do not finish today, you have until the start of class on Tuesday 11/28/2017 to submit it.

Create a file called arrays2.c and paste in the following contents:

#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;
  while (currentIndex < arraySize)
  {
    arrayOfIntegers[currentIndex] = currentIndex;
    currentIndex = currentIndex + 1;
  }

  currentIndex = 0;
  while (currentIndex < arraySize)
  {
    printf("%d\n", arrayOfIntegers[currentIndex]);
    currentIndex = currentIndex + 1;
  }

  return 0;
}

Upload this file to the UMBC Linux Server, compile it and run it.

Modify the program as follows:

  1. Change the program so that the values in the array come from the user. For each position in the array, it should prompt the user for an integer that should be stored at that position.

  2. Add a section of code after the second while loop that calculates the average value stored in the array and then prints the value.

  3. Add a section of code after Part 2 that prints all values in the array that are less than the average value in the array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment