Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Change the program below to ask the user to enter 2 numbers between 0-15 (inclus

ID: 3695387 • Letter: C

Question

Change the program below to ask the user to enter 2 numbers between 0-15 (inclusive, so the values 0 and 15 are valid)

Change the function to allow the user only 5 attempts to input correct data, after that it should return a value of a -1. Hint: Change the outer do-while loop to have it continue while (error && count < 5). After the loop, use an if statement to test if (count >= 5), if it is set num =-1.

Change the main program to test the value returned from each call to the get_int function, if it's = -1 it  should print "You have entered invalid data- program terminated " and terminate the program with a return (0);   else  it should print " The number you entered is: # ".

Report the average of the two numbers.

Don't forget to add a line at the very top: #include "stdafx.h" (needed for Visual Studio software).
Save your program and Build it.

Explanation / Answer

#include "stdafx.h"
#include <stdio.h>

int get_int (int, int);

int main(void)
{
int num1, num2;
double average;
char wait;

num1 = get_int(0,15);
if(num1==-1)
{
printf("You have entered invalid data- program terminated ");
return (0);
}
printf("The number you entered is: %d ",num1);
num2 = get_int(0,15);
if(num2==-1)
{
printf("You have entered invalid data- program terminated ");
return (0);
}
printf("The number you entered is: %d ",num2);
average = (num1 + num2)/2.0;
printf("The average of the numbers %d and %d is: %.2f ", num1, num2, average);
printf("Enter any key to end this program:");
scanf(" %c", &wait);
}

int get_int (int min, int max)
{
int num=-1, /* input - number entered by user */
status,count=0; /* status value returned by scanf */
char skip_ch; /* character to skip */
int error; /* error flag for bad input */

/* Request data from user until a valid value is entered (in_val). */
do
{
/* No errors detected yet. */
error = 0;

/* Get a number from the user. */
printf("Enter an integer in the range from %d to %d inclusive> ", min, max);

/* status will store the number of values entered */
status = scanf("%d", &num);

/* Validate the number. */
if (status != 1) /* in_val didn't get an integer */
{
error = 1;
scanf("%c", &skip_ch);
printf("Invalid character >%c ", skip_ch);
}
else if (num < min || num > max)
{
error = 1;
printf("Number %d is not in range. ", num);
}
/* Skip rest of the characters by reading until a ' ' (enter key) is found. */
do
{
scanf("%c", &skip_ch);
}
while (skip_ch != ' ');
count++;
}
while (error&&count<5);
if(count>=5)num=-1;
return (num);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote