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

Problems P4.2, P4.8, P4.20 & P4.21 from the book c++ for everyone p4.2 Write pro

ID: 3551137 • Letter: P

Question

Problems P4.2, P4.8, P4.20 & P4.21

from the book c++ for everyone



p4.2 Write programs that read a sequence of integer inputs and print

a. The smallest and largest of the inputs.

b. The number of even and odd inputs.

c. Cumulative totals. For example, if the input is 1 7 2 9, the program should print 181019.

d. All adjacent duplicates. For example, if the input is 1 3 3 4 5 5 6 6 2, the program should print 3 5 6.


p4.8 Write a program that reads a set of floating-point values. Ask the user to enter the values, then print

the average of the values.

the smallest of the values.

the largest of the values.

the range, that is the difference between the smallest and largest.

Of course, you may only prompt for the values once.


p4.20 Prime numbers. Write a program that prompts the user for an integer and then prints out all prime numbers up to that integer. For example, when the user enters 20, the program should print

Recall that a number is a prime number if it is not divisible by any number except 1 and itself.

p4.21 Write a program that prints a multiplication table, like this:

1 2 3 4 5 6 7 8 9 10

2 4 6 8 10 12 14 16 18 20

3 6 9 12 15 18 21 24 27 30

...

10 20 30 40 50 60 70 80 90 100


Explanation / Answer

question 1 )


#include <iostream>

#include <cmath>

#include <stdio.h>

#include <stdlib.h>


using namespace std;


void dup(int a[],int n)

{

int i,count=0;

for(i=0;i<n-1;++i)

{

if(i!=0)

{

if(a[i]==a[i-1])

{

if(count==0)

{

count=1;

printf("%d ",a[i]);

}

}

else

count=0;

}

}

return;

}


int main()

{

int i=0;

int a[100000];

int *t=a;

printf("enter the numbers.enter -999999 to stop entering ");

do{

scanf("%d",t);

++t;

++i;

}while(a[i-1]!=-999999);


int n=i;

if(n!=1)

{

int min=10000000,max=-99999999,count_even=0,count_odd=0,cummulative=0;

printf("cummulative average= ");

for(i=0;i<n-1;++i)

{

if(a[i]>max)

max=a[i];

if(a[i]<min)

min=a[i];

if(a[i]%2==0)

count_even++;

if(a[i]%2==1)

count_odd++;

cummulative+=a[i];

printf("%d",cummulative);


}

if(n==2)

{

max=a[0];

min=a[0];

}

printf(" the number of even and odd terms are %d and %d respectively ",count_even,count_odd );

printf(" max=%d min=%d ",max,min);

printf("duplcates = ");

dup(a,n);

}

else

printf("enter atleast one number other than -999999 ");

return 1;

}




question 2)


#include <iostream>

#include <cmath>

#include <stdio.h>

#include <stdlib.h>


using namespace std;



int main()

{

int i=0;

float a[100000];

float *t=a;

printf("enter the numbers.enter -999999 to stop entering ");

do{

scanf("%f",t);

++t;

++i;

}while(a[i-1]!=-999999);


int n=i;

if(n!=1)

{

float min=10000000,max=-99999999,count_even=0,count_odd=0,cummulative=0;

for(i=0;i<n-1;++i)

{

if(a[i]>max)

max=a[i];

if(a[i]<min)

min=a[i];

cummulative+=a[i];


}

if(n==2)

{

max=a[0];

min=a[0];

}

printf(" average = %f ",cummulative/(n-1));

printf(" max=%f min=%f ",max,min);

printf("range(min-max) = %f ",min-max);

}

return 1;

}



question 3)


#include <iostream>

#include <stdio.h>


int main() {

int user_input, x;

printf("Please enter an integer upper bound:" );

scanf("%d",&user_input);

//this for loop goes through each number up to the user input upper boundary

for (int i = 2; i < user_input; i++){// i starts at 2 because the numbers 1 and 2 are by default prime numbers.

x = 0; //a variable to check if a number is prime. If it gets set to 1 the number is not prime.

for (int j = 2; j < i; j++){ //this loop divides the number by 2 up to itself and if the remainder is 0 sets x = 1.

if (i%j == 0){

x = 1;

break;

}

}

if (x == 0){ //if the x = 1 flag is never set the number is prime.

printf("%d ",i);

}

}

return 0;

}




question 4)


#include <iostream>

#include <cmath>

#include <stdio.h>

#include <stdlib.h>


int main()

{

int i,j;

for(i=1;i<11;++i)

{

for(j=1;j<11;++j)

{

printf("%d ", i*j);

}

printf(" ");

}

return 1;

}

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