so the void function below works to calculate the sum and average of all numbers between m and n(inclusive). The problem im having is that if i enter an M value higher than the N value, the program should output a 0. Can anyone help me with this.
}
void fun2(int m, int n)
{
int sum=((n+m)*(n-m+1)/2);
int cnt=n-m+1;
double avg=(double) sum/(double)cnt;
cout<<"for function 2, m ="<< m << " n="<< n<< " sum="<< sum << " avg="<< avg<<endl;
}
we can use an if statement , which checks if m>n , if it is so we print 0.
and inside the else block we can put the whole program
PROGRAM:
void fun2(int m, int n)
{
if (m>n){
cout<< 0;
}
else{
int sum=((n+m)*(n-m+1)/2);
int cnt=n-m+1;
double avg=(double) sum/(double)cnt;
cout<<"for function 2, m ="<< m << "
n="<< n<< " sum="<< sum << " avg="<<
avg<<endl;
}
}
so the void function below works to calculate the sum and average of all numbers between...
Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....
In C++ Write a function that accepts two int arrays of the same size. The first array will contain numbers and the second array will be filled out inside the function. The function should find all numbers in the array that are greater or equal to the average and fill out the second array with these numbers. You need to design the function. I tried this code but the errors returned were: expression must have a constant value #include<iostream> using...
C ++ 1. Write down the missing code according to the comments. #include <cstdlib> #include <iostream> using namespace std; void fun1(int radius) { /* dynamic memory management */ double * dmptr; double circumference; /* add the code below to: - allocate a memory location for a double and nameless variable in heap and assign its address to the pointer - assign 3.14 to the variable in heap via the pointer....
Can some help me with my code I'm not sure why its not working. Thanks In this exercise, you are to modify the Classify Numbers programming example in this chapter. As written, the program inputs the data from the standard input device (keyboard) and outputs the results on the standard output device (screen). The program can process only 20 numbers. Rewrite the program to incorporate the following requirements: a. Data to the program is input from a file of an...
#include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...
Count Below and Above Average Numbers
Use the following main( ) function:
const int maxSize = 50;
int main()
{
int l, u, n, A[maxSize], le, mo;
input(l, u);
cout << "How many numbers you want to generate < 50:
";
cin >> n;
generateNumbers(n, l, u, A);
le = countLessMore(n, A, mo);
cout << "There are " << le << " numbers less
than the average and "
<< mo << " numbers more than the
average" <<...
#include <iostream> #include <string> using namespace std; int main() { int number; int sum = 0; while(true) { cout << "Please enter a number between 1 and 11: "; cin >> number; if (number >= 1 && number <= 11) { cout << number << endl; sum = sum + number; //only add the sum when number is in range: 1-11, so add wthin this if case } else { cout << number << endl; cout << "Out of range;...
Create a program with the main function and one additional function. The additional function asks for input from the user and returns the output back to the main function for display. This is for c++ and here is my code, could you guys please tell me what I am missing from the instructions. Thank you #include <iostream> using namespace std; int sum (int, int); int main() { int n1, n2, total; cout << "Enter two numbers...
c++
• return type function name (parameter); • Ex: int Joe(int, int); • Ex: void gg(int, float); • Ex: double Two_sol(double, double, double): 2. fC → function call • Ex: cout << Joe(); • Ex: Joe(5, 0); 3. fD → function definition return type function name of STATEMENT(S) return function name: **program84 Void functions to print "Welcome to the world of functions!" programs. To print the sum and product of 2 numbers using functions w/out parameters. program86: to print the...