C++ (include comments) There are 3 exercises Exercise 1 (15 points) Write a func
ID: 3760345 • Letter: C
Question
C++ (include comments)
There are 3 exercises
Exercise 1 (15 points)
Write a function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Also, write a program to test your function.
Grading scheme: implement smallestIndex function correctly - 5 points a main function to call smallestIndex - 5 points; print out the right index - 5 points.
Exercise 2 (15 points)
Write a function that takes as parameter a character array, which may contains lowercase, uppercase, digits, and other special symbols (e.g, *, $, ...), and outputs the number of lowercase, uppercase, digits, and special symbol. Please use a character array to store the input string. You can assume the character array is large enough to store all input characters. Also, write a program to test your function.
Example input: AaBbCc123!@#
Output: Lowercase: 3, Uppercase: 3, Digits: 3, Special symbol: 3.
Grading scheme: read and store the input string as a character array - 5 points; a function to count the number of lowercase, uppercase, digits, and special symbol - 5 points; a main function to call your function - 5 points.
Exercise 3 (20 points)
Write a program that uses a two-dimensional array to store the highest and lowest temperature for each month of the year. The program should output the average high, average low temperatures for the year. Your program must consist of the following functions:
a) Function getData: This function prompts the user to enter the data and stores data in the two dimensional array.
b) Function averageHigh: This function calculates and returns the average high temperature for the year.
C) Function averageLow: This function calculates and returns the average low temperature for the year.
Grading scheme: implement getData function - 5 points; implement averageHigh function - 5 points; implement averageLow function - 5 points; a main function to call above three functions and output correct results – 5 points.
Explanation / Answer
Exercise 1:
#include <iostream>
using namespace std;
//Find the smallest number index, given the array and its size.
int smallestIndex(int A[], int size)
{
int smallIndex = 0; //Assume the first digit is smallest.
for(int i = 1; i < size; i++) //From second number to last number.
if(A[i] < A[smallIndex]) //If that number is smaller than the smallIndex positioned number.
smallIndex = i; //Update the small index.
return smallIndex; //Finally return the smallIndex.
}
//Main to test the logic.
int main()
{
int A[100], n;
cout<<"Enter the size of n: ";
cin>>n;
cout<<"Enter "<<n<<" numbers into the array: ";
for(int i = 0; i < n; i++)
cin>>A[i];
int smallIndex = smallestIndex(A, n);
cout<<"The index of the first occurence of smallest element in the array is: "<<smallIndex<<endl;
}
Exercise 2:
#include <iostream>
using namespace std;
//Function takes the character array as input, and
//displays the number of lowercase, uppercase characters,
//number of digits, and number of special symbols.
void CharArrayStats(char Array[20])
{
int numOfLowercases = 0, numOfUppercases = 0, numOfDigits = 0, numOfSymbols = 0;
for(int i = 0; Array[i] != ''; i++) //For every character.
if(islower(Array[i])) //If lowercase, increment relative counter.
numOfLowercases++;
else if(isupper(Array[i])) //If uppercase, increment relative counter.
numOfUppercases++;
else if(isdigit(Array[i])) //If digit, increment relative counter.
numOfDigits++;
else //If symbol, increment relative counter.
numOfSymbols++;
cout<<"Number of lowercase characters is: "<<numOfLowercases<<endl;
cout<<"Number of uppercase characters is: "<<numOfUppercases<<endl;
cout<<"Number of digits is: "<<numOfDigits<<endl;
cout<<"Number of symbols is: "<<numOfSymbols<<endl;
}
//Main to test the function.
int main()
{
char A[20];
cout<<"Enter the input string: ";
cin>>A;
CharArrayStats(A);
}
Exercise 3:
#include <iostream>
using namespace std;
void getData(int Temperatures[12][2])
{
for(int i = 1; i <= 12; i++) //Reading temperatures.
{
cout<<"Enter the lowest temperature of month "<<i<<": ";
cin>>Temperatures[i-1][0];
cout<<"Enter the highest temperature of month "<<i<<": ";
cin>>Temperatures[i-1][1];
}
}
float averageHigh(int Temperatures[12][2])
{
int sum = 0;
for(int i = 0; i < 12; i++)
sum += Temperatures[i][1];
return (float)sum/12.0;
}
float averageLow(int Temperatures[12][2])
{
int sum = 0;
for(int i = 0; i < 12; i++)
sum += Temperatures[i][0];
return (float)sum/12.0;
}
int main()
{
int Temperatures[12][2];
cout<<"Reading temperatures..."<<endl;
getData(Temperatures);
float avgHigh = averageHigh(Temperatures);
float avgLow = averageLow(Temperatures);
cout<<"The average of highest temperatures is: "<<avgHigh<<endl;
cout<<"The average of lowest temperatures is: "<<avgLow <<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.