Skip to content

Instantly share code, notes, and snippets.

@rszewczyk
Last active November 7, 2017 22:19
Show Gist options
  • Save rszewczyk/1dda5eac32f62eace3eceb7115fa4c96 to your computer and use it in GitHub Desktop.
Save rszewczyk/1dda5eac32f62eace3eceb7115fa4c96 to your computer and use it in GitHub Desktop.
CMSC 104 - Class 18

In class assignment

Finish the in class assigment from class 17 if you haven't already done so. You are strongly encouraged to complete the optional practice problems as well.

Write a program that prompts the user for two numbers, each of which represents the radius of a circle. Your program will then calculate the difference in areas if you subtract the area of the smaller circle from the area of the bigger circle. Your program will then print the difference then ask the user if they wish to do another calculation. If they type the character y, the program should repeat. If they type any other character, it should exit. Save your program in a file called circles.c and submit it on slack (HINT: yes, you should use a function to handle calculating the area of the circles and yes, you should just use the one that was presented as an example in a previous class).

Another Loop Example

Problem

Write a C program that prompts the user for an integer greater than zero. If the user enters an negative value, inform them and prompt them again. After the user has entered the correct value calculate the sum of all integers that are less than or equal to the value entered by the user.

Solution

Solve this step by step by getting one piece at a time to work.

Step 1

As always, the first step is to create the basic shell of a C program:

#include <stdio.h>

int main()
{
  return 0;
}

Step 2

Next, prompt the user for a value, get the value and print a message if the value is invalid:

#include <stdio.h>

int main()
{
  int valueFromUser = 0;

  printf("Type an integer that is greater than 0 and press return: ");
  scanf("%d", &valueFromUser);

  if (valueFromUser < 1)
  {
    printf("%d is not greater than 0\n", valueFromUser);
  }

  return 0;
}

Step 3

Now, handle reprompting the user if the enter an invalid value

#include <stdio.h>

int main()
{
  int valueFromUser = 0;

  while (valueFromUser < 1)
  {
    printf("Type an integer that is greater than 0 and press return: ");
    scanf("%d", &valueFromUser);

    if (valueFromUser < 1)
    {
      printf("%d is not greater than 0\n", valueFromUser);
    }
  }

  return 0;
}

Step 4

Now, use the value from the user to set up the next loop that will produce successive integers up to and including the user's value. For now just print the value so that you know it's correct:

#include <stdio.h>

int main()
{
  int valueFromUser = 0;

  while (valueFromUser < 1)
  {
    printf("Type an integer that is greater than 0 and press return: ");
    scanf("%d", &valueFromUser);

    if (valueFromUser < 1)
    {
      printf("%d is not greater than 0\n", valueFromUser);
    }
  }

  int currentValue = 1;

  while (currentValue <= valueFromUser)
  {
    printf("%d\n", currentValue);
    currentValue = currentValue + 1;
  }

  return 0;
}

Step 5

Now, add code to keep track of the cumulative value and print it when finished.

#include <stdio.h>

int main()
{
  int valueFromUser = 0;

  while (valueFromUser < 1)
  {
    printf("Type an integer that is greater than 0 and press return: ");
    scanf("%d", &valueFromUser);

    if (valueFromUser < 1)
    {
      printf("%d is not greater than 0\n", valueFromUser);
    }
  }

  int currentValue = 1;
  int cumulativeValue = 0;

  while (currentValue <= valueFromUser)
  {
    cumulativeValue = cumulativeValue + currentValue;
    currentValue = currentValue + 1;
  }

  printf("The sum of positive integers up to an including %d is %d\n", valueFromUser, cumulativeValue);

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