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

I need help with this problem, Using C programming not C++. This program will us

ID: 671322 • Letter: I

Question

I need help with this problem, Using C programming not C++.

This program will use a loop to process a collection of integers entered by the user. Be sure to test your program after each step! Write a program that uses a counter-controlled while loop (within main - no functions necessary in this lab) to process a collection of integers entered by the user. Set the counter and loop condition so that the loop iterates 15 times. The program should initially just get a number from the user and echo it to the console (output) within the loop. Test and run your program. Verify that it prompts for and displays exactly 15 integers. Modify your program so that it calculates the total of the numbers entered, and displays that total at the end of the program. (If you enter the integers 1-15, the total should be 120.) Modify your prompt that asks the user to enter an integer so that it asks the user to enter an integer between 5 and 20 (inclusive on both ends). Add an input verification loop immediately after the input statement to ensure that the user input is valid before processing the value. The loop test should be true if the value is NOT valid. The body of the loop should state the input value was invalid and should allow the user to re-enter the value. Test your program by entering values that are not in the expected range. Be sure to enter several consecutives values that are incorrect. Your program should still allow the user to input 15 "correct" values (the incorrect values should not be counted). Using a for loop Create a new program file, yourlastname_lab5_part2.c. Copy and paste your code from part 1 into this new program file. Remove all code pertaining to step 3 above. The program should now' accept any integer input value. Change the counter-controlled while loop to a for loop. Verify that your program still works correctly. Use redirection Modify your program so that the counter-controlled loop iterates 50 times. Create a text file named numbers.txt, in the same location as your program file, that contains 50 integers. The integers may be on the same line or on separate lines, as long as there is white space between them. Use redirection, as described on the next page, to run your program using the text file as input. Demonstrate this step to your instructor.

Explanation / Answer

Part 3 Answer:

#include <stdio.h>

#include <conio.h>

int main()

{

FILE *fp = fopen("input.txt", "r");

    int i;

    int a[50];

    for (i = 0; i < 50; i++) {

        fscanf(fp, "%d", &a[i]);

    }

    fclose(fp);

    return 0;

}

#include <stdio.h>

#include <conio.h>

int main()

{

FILE *fp = fopen("input.txt", "r");

    int i;

    int a[50];

    for (i = 0; i < 50; i++) {

        fscanf(fp, "%d", &a[i]);

    }

    fclose(fp);

    return 0;

}

Part 2 counter answer

#include <stdio.h>
#include <conio.h>
int main()
{
int i=0, sum=0;
int a[15];

printf("enter 15 integers");                       //First point
while(i<15)
{
    scanf("%d",&a[i]);
    printf(" Entered number is %d ",a[i]);
    i++;
}

i=0;
while(i<15)                                        //Second point
{
     sum=sum+a[i];
     i++;
}
printf(" Sum of all the digits are : %d ",sum);


i=0;
printf("enter 15 integers between 5 and 20 only"); //Thrid point
while(i<15)
{
     scanf("%d",&a[i]);

      if(a[i]>=5 && a[i]<=20)
        i++;
     else
        printf(" Number entered should be between 5 and 20. Please enter again ");


   /* while(a[i]<=5 && a[i]>=20)
    {
        printf("Number entered should be between 5 and 20. Please enter again");
        scanf("%d",&a[i]);

    }
    i++; */
}

i=0;
while(i<15)
{
    printf(" You entered %d",a[i]);
    i++;
}
getch();
}

Part 1 Answer

#include <stdio.h>
#include <conio.h>
int main()
{
int i=0, sum=0;
int a[15];

printf("enter 15 integers");                       //First point
for(i=0;i<15;i++)
{
    scanf("%d",&a[i]);
    printf(" Entered number is %d ",a[i]);
}

for(i=0;i<15;i++)                                        //Second point
{
     sum=sum+a[i];
     i++;
}
printf(" Sum of all the digits are : %d ",sum);

getch();
}