Question

In: Computer Science

Q#1 Write a C++ program that reads n integer values and stores them in an array...

Q#1 Write a C++ program that reads n integer values and stores them in an array of maximum 20 integers. Read from the user a valid value of n (n should not exceed 20). After reading the n array elements find the maximum and minimum elements. Q#2 Write a C++ program that reads 10 integer values and stores them in an array. The program should find and display the average of the array elements and how many elements are below the average. Q#3 Write a C++ program to read 10 temperatures in Celsius and to store the temperatures in an array of integers, then it should convert the temperatures to Fahrenheit and store the new values rounded to the nearest integer in another array . The program should display both temperatures in a table form. F = 9/5 x C + 32 where F is temp in Fahrenheit and C temperature in Celsius

Solutions

Expert Solution

Q#1 Write a C++ program that reads n integer values and stores them in an array of maximum 20 integers. Read from the user a valid value of n (n should not exceed 20). After reading the n array elements find the maximum and minimum elements.

C++ Program

#include<iostream>
using namespace std;

int main ()
{
  
int values[20], size, i, maximum, minimum;
  
cout << "Enter the size of the array : ";
cin >> size;
  
if(size>20)
{
cout << "Error ! Array size should not exceed 20" << endl;
return 0;
}
else
{
cout << "Enter " << size << " elements of the array one below the other :: " << endl;
for (i = 0; i < size; i++)
cin >> values[i];

/** Assign the first element in the array as the maximum element */
maximum = values[0];
/** check all the other values with the maximum to see if any other number
* is greater than maximum. If so, assign that value to maximum */
for (i = 0; i < size; i++)
{
if (maximum < values[i])
maximum = values[i];
}
  
/** Assign the first element in the array as the minimum element */
minimum = values[0];
/** check all the other values with the minimum to see if any other number
* is smaller than minimum. If so, assign that value to minimum */
for (i = 0; i < size; i++)
{
if (minimum > values[i])
minimum = values[i];
}
  
cout << "Maximum element : " << maximum << endl;
cout << "Minimum element : " << minimum << endl;
return 0;
}
  
}

========================================================================================

============================================================================================

Output:

=======================================================================================

Q#2 Write a C++ program that reads 10 integer values and stores them in an array. The program should find and display the average of the array elements and how many elements are below the average.

#include<iostream>
using namespace std;

int main ()
{
  
int values[10], i;
int sum = 0;
float average = 0;
int count = 0;
  
cout << "Enter 10 elements of the array one below the other :: " << endl;
for (i = 0; i < 10; i++)
cin >> values[i];
  
/* add all the elements of the array to the variable sum */   
for (i = 0; i < 10; i++)
{
sum = sum + values[i];
}
/* find the average by dividing sum by 10.0 */
average = sum / 10.0;
  
/** check all the elements of the average. If the values are less than
* average, increment the count */
for (i = 0; i < 10; i++)
{
if(values[i] < average)
count++;
}

cout << "Average : " << average << endl;
cout << "Count of elements below the average : " << count << endl;
return 0;

}

======================================================================================

=============================================================================================

=============================================================================================

Q#3 Write a C++ program to read 10 temperatures in Celsius and to store the temperatures in an array of integers, then it should convert the temperatures to Fahrenheit and store the new values rounded to the nearest integer in another array . The program should display both temperatures in a table form. F = 9/5 x C + 32 where F is temp in Fahrenheit and C temperature in Celsius.

#include<iostream>
#include "math.h"
using namespace std;

int main ()
{
  
int fahrenheit[10], celsius[10];
int sum = 0, i;
float average = 0;
int count = 0;
  
cout << "Enter 10 temperatures in Celsius one below the other :: " << endl;
for (i = 0; i < 10; i++)
cin >> celsius[i];
  
/* convert all celsius values to fahrenheit and store it in another array */   
for (i = 0; i < 10; i++)
{
fahrenheit[i] = round((9/5.0) * celsius[i]) + 32;
}
  
cout << "CELSIUS" << "\t" << "FAHRENHEIT" << endl;
cout << "=======" << "\t" << "==========" << endl;
for (i = 0; i < 10; i++)
{
cout << celsius[i] << "\t" << fahrenheit[i] << endl;
}
return 0;

}

======================================================================================

==========================================================================================


Related Solutions

Q#2 Write a C++ program that reads 10 integer values and stores them in an array....
Q#2 Write a C++ program that reads 10 integer values and stores them in an array. The program should find and display the average of the array elements and how many elements are below the average.
C++ Language Write a program that reads the numbers.txt file and stores it into an array...
C++ Language Write a program that reads the numbers.txt file and stores it into an array of integers. Use the sample functions in the text and in the notes from chapter 8 to sort the array in ascending order (low to high). You can use either method (bubble or selection sort) to accomplish this. Then store the sorted array into an output file sorted Numbers.txt. There are 100 values both positive and negative integers in this file.
Write a program that reads n integer values. If a negative value is entered, we want...
Write a program that reads n integer values. If a negative value is entered, we want to terminate the input, i.e., exit from the loop. If a zero value is entered, we want to ignore it and read the next value. Any strictly positive values (greater or equal zero) are to be totaled. Print the number of values read, the number of values totaled and the total. If a negative value is entered, print an error message before terminating the...
Write a C program to Declare an integer array of size 10 with values initialized as...
Write a C program to Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9])...
Write a C program that asks the user to enter 15 integer numbers and then store them in the array.
Write a C program that asks the user to enter 15 integer numbers and then store them in the array. Then, the program will find the second largest element in array and its index without sorting the array. For example, In this array {-55,-2,1, 2, -3, 0, 5, 9, 13, 1, 4, 3, 2, 1, 0}, the second largest element is 9 [found at index 7].
In c++ Write a program that reads a string consisting of a positive integer or a...
In c++ Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. Use the STL stack
In c++, write a program that reads a string consisting of a positive integer or a...
In c++, write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format.
Write a C program that reads an integer value. Assume it is the number of a...
Write a C program that reads an integer value. Assume it is the number of a month of the year; print out the name of that month (Hint: months need to be captured in an array).
Write a program that reads a positive integer n , prints all sums from 1 to...
Write a program that reads a positive integer n , prints all sums from 1 to any integer m 1≤m≤n . For example, if n=100, the output of your program should be The sum of positive integers from 1 to 1 is 1;       The sum of positive integers from 1 to 2 is 3;       The sum of positive integers from 1 to 3 is 6;       The sum of positive integers from 1 to 4 is 10;      ...
In C ++, Design and implement a program that reads from the user four integer values...
In C ++, Design and implement a program that reads from the user four integer values between 0 and 100, representing grades. The program then, on separate lines, prints out the entered grades followed by the highest grade, lowest grade, and averages of all four grades. Format the outputs following the sample runs below. Sample run 1: You entered:    95, 80, 100, 70 Highest grade: 100 Lowest grade:   70 Average grade: 86.25
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT