Question

. . Note: The order that these functions are listed, do not reflect the order that they should be called. Your program must b Please Use C++ for coding
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;

int sumOfSquares(int arr[],int n)
{
int sum=0;
for(int i=0;i<n;i++)
{
sum+=arr[i]*arr[i];
}
return sum;
}
int countMult5(int a,int b,int arr[],int n)
{
int count=0;
for(int i=0;i<n;i++)
{
if(arr[i]>a && arr[i]<b)
count++;
}
return count;
}
void printOdd(int arr[],int n)
{
for(int i=0;i<n;i++)
{
if(arr[i]%2!=0)
cout<<arr[i]<<endl;
}
}
int productEven(int a,int b,int arr[],int n)
{
int prod=1;
for(int i=0;i<n;i++)
{
if(arr[i]>a && arr[i]<b && arr[i]%2==0)
{
prod*=arr[i];
}
}
return prod;
}
float calcAverage(int arr[],int n)
{
int sum=0;
for(int i=0;i<n;i++)
{
sum+=arr[i];
}
return(sum/n);
}
int multiply(int a,int b)
{
return a*b;
}
int randCount()
{
int n = 0,count=0;

for (int i = 0; i < 20; i++)
{
n = 8 + (rand() % (int)(8));
if(n%2==0)
count++;
}
return(count);
}

int main()
{
int arr[100],j=0,choice,a,b;
string i;
   ifstream rf;
   rf.open("sample.txt");
   while (rf) {
       getline(rf, i);
       istringstream(i)>>arr[j++];
   }
   rf.close();
   while(1)
{
cout<<"1.Print sum of squares"<<endl;
cout<<"2.Print count of total number which are multiple of 5"<<endl;
cout<<"3.Print Odd numbers"<<endl;
cout<<"4.Product of all even numbers"<<endl;
cout<<"5.Average"<<endl;
cout<<"6.Multiply"<<endl;
cout<<"7.randCount"<<endl;
cout<<"8.Exit"<<endl;
cout<<"Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
cout<<sumOfSquares(arr,j)<<endl;
break;
case 2:
cout<<"Enter two numbers to count in between:";
cin>>a>>b;
cout<<"\n"<<countMult5(a,b,arr,j)<<endl;
break;
case 3:
printOdd(arr,j);
break;
case 4:
cout<<"Enter two numbers to calc in between:";
cin>>a>>b;
cout<<productEven(a,b,arr,j)<<endl;
break;
case 5:
cout<<calcAverage(arr,j)<<endl;
break;
case 6:
cout<<"Enter two numbers to calc in between:";
cin>>a>>b;
cout<<multiply(a,b)<<endl;
break;
case 7:
cout<<randCount()<<endl;
break;
case 8:
exit(0);

}
}

   return 0;
}

C:\Users\AMIR KHAN\Desktop\C++\chg\bin\Debug\chg.exe 1.Print sum of squares 2.Print count of total number which are multiplC:\Users\AMIR KHAN\Desktop\C++\chg\bin\Debug\chg.exe 7.randCount 8. Exit Enter your choice:4 Enter two numbers to calc in b

Add a comment
Know the answer?
Add Answer to:
Please Use C++ for coding . . Note: The order that these functions are listed, do...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Note: The order that these functions are listed, do not reflect the order that they should...

    Note: The order that these functions are listed, do not reflect the order that they should be called. Your program must be fully functional. Submit all .cpp, input and output files for grading. Write a complete program that uses the functions listed below. Except for the printOdd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention to the order of your function...

  • •The multiplication operator works by summing some value, x, by another value, y. Write a function...

    •The multiplication operator works by summing some value, x, by another value, y. Write a function definition called multiply that takes two integer parameters to perform the operation described. Return the answer to the calling function. •Write a function called randCount with no parameters. The function should randomly generate 5 numbers between 8 to 15 and counts how many times a multiple of 2 occurred. Return the answer to the calling function. •Write a function called summary that takes as...

  • Write a complete program that uses the functions listed below. Except for the printOdd function, main...

    Write a complete program that uses the functions listed below. Except for the printOdd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention to the order of your function calls. Be sure to read in data from the input file. Using the input file provided, run your program to generate an output file. Upload the output file your program generates. •Write a...

  • Write a function in the C++called summary that takes as its parameters an input and output...

    Write a function in the C++called summary that takes as its parameters an input and output file. The function should read two integers find the sum of even numbers, the sum of odd numbers, and the cumulative product between two values lower and upper. The function should return the sum of even, odd, ad cumulative product between the lower and upper values. Please write program in C++.

  • please write program in C++ Write a function called productEven, that takes as its parameter an...

    please write program in C++ Write a function called productEven, that takes as its parameter an input file. The function should read two integers and calculate the total product of only even numbers between them. Return the answer to the calling function. please write the program in C++

  • Write a function called productEven that takes as its parameter an input file. The function should...

    Write a function called productEven that takes as its parameter an input file. The function should read two integers and calculate the total product of only even numbers between them. Return the answer to the calling function. c++ program

  • in Java and also follow rubric please 4. Write a complete program to do the following:...

    in Java and also follow rubric please 4. Write a complete program to do the following: Using an input and output files, write a program that will read 20 numbers from an input file called InFile. Sum all even numbers and multiply all odd numbers. Print the numbers read from the input file to an output file called OutFile. Also print the sum of the even numbers and the product of the odd numbers to the output file. Rubric: •...

  • Language is C++ NOTE: No arrays should be used to solve problems and No non-constants global...

    Language is C++ NOTE: No arrays should be used to solve problems and No non-constants global variables should be used. PART B: (Statistics Program) – (50%) Please read this lab exercise thoroughly, before attempting to write the program. Write a program that reads the pairs of group number and student count from the text file (user must enter name of file) and: Displays the number of groups in the file (5 %) Displays the number of even and odd student...

  • use C++ please 1. Vowels and Consonants Write a program that asks the user to input...

    use C++ please 1. Vowels and Consonants Write a program that asks the user to input three different integers. Write a function called numberStyle for this program that will accept each integer (one at a time) and return the following . If the integer is even, return 1 . If the integer is odd, return -1 . If the integer is zero, return O In main, after calling the function output the appropriate message describing the integers ing the function...

  • FUNCTIONS In this assignment, you will revisit reading data from a file, and use that data...

    FUNCTIONS In this assignment, you will revisit reading data from a file, and use that data as arguments (parameters) for a number of functions you will write. You will need to: Write and test a function square_each(nums) Where nums is a (Python) list of numbers. It modifies the list nums by squaring each entry and replacing its original value. You must modify the parameter, a return will not be allowed! Write and test a function sum_list(nums) Where nums is a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT