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

co P2270 by Tcunmyngham Chapter 04 Program Exercises PE 04 01 (Even and Odd Tota

ID: 3844286 • Letter: C

Question

co P2270 by Tcunmyngham Chapter 04 Program Exercises PE 04 01 (Even and Odd Totals) Write a program using a for loop that calculates the sum 1) of even and odd integers from l to 100. Use a switch statement in the for body. Note that this is not the most efficient method. Summarize the totals of all even and odd numbers. 2) PE 04.02 (Sequence of Integers) Write a counter-controlled program using a for loop that determines the average, sum, and product of a sequence of integers that range from 1 to 20. The user should specify the number of integers n to enter. This program is not required to verify that the input is in the correct range (1 s input s 20), but verification is encouraged. 3) PE 04.03 (Grade Summary) Write a sentinel controlled program using a do while that allows the user to enter any number of integer grades (sentinel 1) The program should also check that grades are entered correctly (0 S grade s 100), and terminate properly if the user enters the sentinel (-1) first. Calculate and neatly summarize the following: sum of all grades, total number of valid grades entered, largest grade. smallest grade. and average of all grades. 4) PE 04 04 (Do I Form rsion of this program. The enter the n determines MacBook Air

Explanation / Answer

The three exercises in C are as follows:

1.PE_04_01 (Even and Odd totals)

#include<stdio.h>

void main(){

   int evensum;
   int oddsum;
   int found;

   oddsum = 0;
   evensum = 0;
   for (int i = 1; i<=100; i++) {
       found = 0;
       if (i % 2 == 0)
          found = 1;
       switch(found) {
            case 1:
              evensum = evensum + i;
            case 0:
              oddsum = oddsum + i;
       }
     
   }
   printf("Sum of odd numbers: %d ", oddsum);
   printf("Sum of even numbers: %d ", evensum);
}


2.PE_04_02 (Sequence of integers)

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

void main(){

   float avg;
   int sum;
   int product;
   int n;
   int num;

   do {
      printf("Enter n:");
      scanf("%d", &n);
      if ( n < 1 || n > 20)
         printf("Please enter value from 1 to 20 ");
     
   } while (n < 1 || n > 20)
   sum = 0;
   product = 1;
   srand((unsigned)time(NULL));
   for (int i = 0; i<n; i++) { // we will generate n random numbers between 1 and 20
      num = rand() % 20 + 1
      sum = sum + num;
      product = product * num;
   }
   printf("Average: %f ", sum/n);
   printf("Sum: %d ", sum);
   printf("Product: %d ", product);
}

3. PE_04_03 (Grade Summary)

void main(){

   float avg;
   int sum;
   int grade;
   int count_valid

   count_valid = 0;
   max = 0;
   min = 101;
   sum = 0;
   do {
      printf("Enter grade:");
      scanf("%d", &grade);
      if ( grade >= 0 || grade <= 100){ //Ignoring the invalid grades
         count_valid++;
         sum = sum + grade;
         if (grade > max)
            max = grade;
         if (grade < min)
            min = grade;
      }
   } while (grade != -1)
   printf("Sum: %d ", sum);
   printf("Total number of valid grades: %d ", count_valid);
   printf("largest grade: %d ", max);
   printf("smallest grade: %d ", min);
   printf("Average: %f ", sum/count_valid);
}