Single File Programming QuestionProblem StatementMithun is developing a program for a data collection tool that allows the user to input a series of positive integers until they enter a negative value. The program should dynamically allocate memory for an integer array to store these numbers using the new operator. The program should then calculate and display the sum and average of the entered integers. Write a program to accomplish this task.Note: This kind of question will help in clearing HCL recruitment.Input format :The input consists of positive integers separated by a space. It continues until a negative value is entered.Output format :The first line of the output displays the entered numbers.The second line displays the sum of the numbers.The third line displays the average of the entered numbers.Refer to the sample output for formatting specifications.Code constraints :numbers > 0Maximum number <=100Sample test cases :Input 1 :1 2 3 4 5 6 -1Output 1 :Numbers entered: 1 2 3 4 5 6 Sum of entered numbers: 21Average of entered numbers: 3.50Input 2 :10 20 30 40 50 60 70 80 90 100 -4Output 2 :Numbers entered: 10 20 30 40 50 60 70 80 90 100 Sum of entered numbers: 550Average of entered numbers: 55.00Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.
Question
Single File Programming QuestionProblem StatementMithun is developing a program for a data collection tool that allows the user to input a series of positive integers until they enter a negative value. The program should dynamically allocate memory for an integer array to store these numbers using the new operator. The program should then calculate and display the sum and average of the entered integers. Write a program to accomplish this task.Note: This kind of question will help in clearing HCL recruitment.Input format :The input consists of positive integers separated by a space. It continues until a negative value is entered.Output format :The first line of the output displays the entered numbers.The second line displays the sum of the numbers.The third line displays the average of the entered numbers.Refer to the sample output for formatting specifications.Code constraints :numbers > 0Maximum number <=100Sample test cases :Input 1 :1 2 3 4 5 6 -1Output 1 :Numbers entered: 1 2 3 4 5 6 Sum of entered numbers: 21Average of entered numbers: 3.50Input 2 :10 20 30 40 50 60 70 80 90 100 -4Output 2 :Numbers entered: 10 20 30 40 50 60 70 80 90 100 Sum of entered numbers: 550Average of entered numbers: 55.00Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.
Solution
Here is a simple C++ program that accomplishes the task:
#include <iostream>
#include <vector>
#include <iomanip>
int main() {
std::vector<int> numbers;
int input;
std::cin >> input;
while(input >= 0) {
numbers.push_back(input);
std::cin >> input;
}
int sum = 0;
for(int i = 0; i < numbers.size(); i++) {
sum += numbers[i];
}
double average = static_cast<double>(sum) / numbers.size();
std::cout << "Numbers entered: ";
for(int i = 0; i < numbers.size(); i++) {
std::cout << numbers[i] << " ";
}
std::cout << std::endl;
std::cout << "Sum of entered numbers: " << sum << std::endl;
std::cout << std::fixed << std::setprecision(2) << "Average of entered numbers: " << average << std::endl;
return 0;
}
This program reads integers from the user until a negative number is entered. It stores these numbers in a dynamically allocated array (implemented as a std::vector<int>). It then calculates the sum and average of the numbers and prints them out. The average is printed with two decimal places of precision.
Similar Questions
Single File Programming QuestionProblem StatementRaveena is developing a program to analyze an integer array. Her task is to identify and tally the negative numbers within this array. This analysis is crucial for Raveena's dataset, as negative values might represent specific conditions or anomalies that require further investigation. Write a program that assists Raveena in counting and displaying the number of negative elements in an integer array. Input format :The first line of input consists of an integer,n, representing the number of elements in the array.The second line of input consists of n space-separated integers, representing the elements of the array.Output format :If there are no negative elements in the array, the output prints "No negative elements in the array".Otherwise, the output prints "Total negative elements in array = " followed by an integer which is the total count of negative elements in the array.Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases will fall under the following constraints:3 ≤ n ≤ 10-1000 ≤ elements ≤ 1000Sample test cases :Input 1 :1010 -2 5 -20 1 50 60 -50 -12 -9Output 1 :Total negative elements in array = 5Input 2 :56 3 2 1 9Output 2 :No negative elements in the arrayInput 3 :3-1000 -99 1000Output 3 :Total negative elements in array = 2Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.
Single File Programming QuestionProblem StatementHelen is developing a program that analyzes numerical inputs. The program categorizes each input based on its value: a positive number elicits a specific message, a negative number prompts a different response, and inputting zero generates a unique message.Create a program that identifies the type of a number inputted. Ensure the program exits using a return statement after identifying the type of the number.Input format :The input consists of a single integer, N, which represents the number.Output format :The output displays one of the following:If the given number is positive, then display the statement "The number is positive."If the given number is negative, then display the statement "The number is negative."If the given number is zero, then display the statement "The number is zero."Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:-100 ≤ N ≤ 100Sample test cases :Input 1 :100Output 1 :The number is positive.Input 2 :-100Output 2 :The number is negative.Input 3 :0Output 3 :The number is zero.
Single File Programming QuestionProblem StatementJamie is managing a school and is looking to find out the number of students in the class based on the sum of their scores and the average score.He wants to design a program that takes the average score and sum of scores as input and outputs the calculated number of students in the class using pointers.Input format :The first line of input consists of an integer value n, representing the sum of scores.The second line of input consists of a floating-point value f, representing the average score.Output format :The output displays the calculated number of students in the class.Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ n ≤ 1051.0 ≤ f ≤ 100.0Sample test cases :Input 1 :187575.45Output 1 :24Input 2 :20010.25Output 2 :19Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.Marks : 10Negative Marks : 0WhitelistSet 1:*
Single File Programming QuestionProblem StatementPriya is fascinated by maths and wants to calculate the value for the given expression: ++a * ++b / a. The program takes these 2 values as input, calculates the expression, and displays the result.Can you help her by writing a program?Input format :The first line consists of a integer value a, representing the first value.The second line consists of a integer value b, representing the second valueOutput format :The output displays a result as integer.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:1<=a<=10001<=b<=1000Sample test cases :Input 1 :44Output 1 :5Input 2 :105Output 2 :6Input 3 :120500Output 3 :501
Single File Programming QuestionProblem StatementGokul is fascinated by numbers and wants to categorize them. Write a program to input an array of integers, then count and print the number of positive, negative, and zero elements. Implement a function countAndPrint that takes the array as a parameter and categorizes the numbers.Input format :The first line consists of an integer n, representing the size of the array.The next line consists of n space-separated integers, representing the array elements.Output format :The first line displays "Positive: " followed by an integer, representing the count of positive elements.The second line displays "Negative: " followed by an integer, representing the count of negative elements.The third line displays "Zero: " followed by an integer, representing the count of zeros.Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:3 ≤ n ≤ 10-100 ≤ Each element ≤ 100Sample test cases :Input 1 :810 20 -20 -10 33 47 55 0Output 1 :Positive: 5Negative: 2Zero: 1Input 2 :1012 23 45 86 78 -20 -88 -44 -46 -58Output 2 :Positive: 5Negative: 5Zero: 0Input 3 :3-100 0 100Output 3 :Positive: 1Negative: 1Zero: 1
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.