c++ programming .c format stdio.h No iostream. thanks!! ETHeading 1 Normal 1No S
ID: 3699642 • Letter: C
Question
c++ programming
.c format
stdio.h
No iostream.
thanks!!
Explanation / Answer
The code for the first program concerning the random generated array is :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int main()
{
printf("The following options are availble: ");
printf("L : To calculate the maximum number ");
printf("M : To find the minimum number ");
printf("P : To find product of the numbers ");
printf("Q : TO find the sum of square root of each number ");
printf("S : To find the sum of all numbers ");
printf("E : To exit "); // extra case to exit the while loop
while (1)
{
srand(time(0)); // to seed the random function with the current time
int i;
int arr[7]; // to intialize an array
char ch;
for (i = 0; i < 7; i++)
{
arr[i] = (rand() % (20)) + 5; // to store random integers in the given limit in the array
printf("%d ", arr[i]); // to print the numbers simultaneously
}
printf(" ");
printf("Enter the desired option : ");
scanf("%c%*c", &ch); // the extra *c is to avoid the scanf problem with while loop, without it it skips the scanf every other loop
switch (ch)
{
case 'L':
{
int max = 0;
for (i = 0; i < 7; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
printf("The largest number is %d ", max);
break;
}
case 'M':
{
int min = 26;
for (i = 0; i < 7; i++)
{
if (arr[i] < min)
{
min = arr[i];
}
}
printf("The smallest number is %d ", min);
break;
}
case 'P':
{
long int mul = 1; // using long long so as to prevent overflow
for (i = 0; i < 7; i++)
{
mul *= arr[i];
}
printf("The product of all the numbers is %ld ", mul);
break;
}
case 'Q':
{
double sum = 0; // using double as sqrt() has output as double
for (i = 0; i < 7; i++)
{
sum += sqrt(arr[i]);
}
printf("The sum of square root of each of the numbers is %lf ", sum);
break;
}
case 'S':
{
int sum = 0;
for (i = 0; i < 7; i++)
{
sum += arr[i];
}
printf("The sum of all the numbers is %d ", sum);
break;
}
case 'E':
{
printf("Exited successfully "); //This has to be specified or else it will consider 'E' in the default case
break;
}
default:
{
printf("Please enter correct value ");
}
}
if (ch == 'E') // condition to exit the loop
break;
}
return 0;
}
The code for the ISBN problem is as follows :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int main()
{
int isbn[10];
printf("Enter each digit seperately on new lines : "); // Hit enter after every single digit
int i;
for(i = 0; i < 10; i++)
{
scanf("%d", &isbn[i]);
}
printf(" ");
int factor[10];
printf("Enter the weighting factors seperately on new lines : "); // Hit enter after every input
for(i = 0;i < 10; i++)
{
scanf("%d", &factor[i]);
}
printf(" ");
int value[10];
int sum = 0;
for(i = 0;i < 10; i++)
{
value[i] = isbn[i] * factor[i]; // getting weighted values by multiplying
sum += value[i]; // computing the sum simultaneously
}
if(sum % 11 == 0) // checking if the sum of weighted values with modulo 11
{
printf("Valid ISBN number ");
}
else
{
printf("Invalid ISBN number ");
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.