Need help in C++ I got stuck in question 2 and 3 1. Declare an array named tempe
ID: 3803747 • Letter: N
Question
Need help in C++
I got stuck in question 2 and 3
1. Declare an array named temperatures and initialize it with the values 78.9, 32.6, 50.2 IN TWO WAYS (once using static initialization and once NOT using static initialization):
Non-Static Initialization:
Const int SIZE = 3;
int temperatures [SIZE];
temperatures[0]=78.9;
temperatures[1]=32.6;
temperatures[2]=50.2;
Static Initialization:
int temperatures [] = { 78.9, 32.6, 50.2 };
2. Write a for loop to print the contents of the temperatures array from question 1 to the console:
3. Write a function to print the contents of an array of doubles to the console. The function is named printArray. It takes in two parameters - one is an array of doubles and the other parameter is an integer for the size of the array. It prints out the contents of the array. It returns nothing.
Explanation / Answer
C++ Code:
1) use float or double instead of int otherwise your array will be initialized with integers 78,32,50 which I guess is not what you want.
2) for(int i=0;i<3;i++){
cout<<temperatures[i]<<" ";
}
3) void printArray(double arr[],int size){
for(int i=0;i<size;i++){
cout<<arr[i]<<" ";
}
}
//*******************************************************************
#include <iostream>
using namespace std;
void printArray(double arr[],int size){
for(int i=0;i<size;i++){
cout<<arr[i]<<" ";
}
}
int main()
{
double temperatures[]={78.9, 32.6, 50.2};
for(int i=0;i<3;i++){
cout<<temperatures[i]<<" ";
}
cout<<endl;
printArray(temperatures,3);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.