In: Computer Science
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
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;
}
======================================================================================
==========================================================================================