-5

PART 1 Write a program that will:
1.Call a function to input temperatures for consecutive days in an array. The temperatures are to be integer numbers.
2.The user will input the number of temperatures. There will be no more than 10 temperatures.
3.Call a function to sort the array by ascending order. You can use any sorting algorithm you wish as long as you can explain it.
4.Call a function that will return the average of the temperatures. The average should be displayed to two decimal places.

Below is my source code, am I on the right track?
What algorithm should i use for this question?
For the number 4, I could just use the average function right?
Also is there any youtube channels or other learning sources that you would recommend for a beginner to study C++? I have tried reading the books but I did not feel that really helps me.
I am a beginner and I have tried searching it online but I did not find any solution.

#include <iostream>
using namespace std;
int main()
{
    int numtemp;
    cout<<"Please input the number of temperatures to be read"<<endl;
    cin>>numtemp;
    int temp[numtemp];
    for (int i=0;i<numtemp;i++)
    {
        cout<<"Please input temperatures for day "<<(i+1)<<endl;
        cin>>temp[i];
    }
    return 0;
}
Andrew
  • 1
  • 1
  • 1
    [The Definitive C++ Book Guide and List](https://stackoverflow.com/a/388282/7076153) – Stargateur Jun 28 '17 at 01:40
  • 1
    I don't think that VLA is standard in C++, you should use a vector. – Stargateur Jun 28 '17 at 01:41
  • This looks right, there are many sorting algorithms you can use for Q3, if you do a little search. and for Q4, I do not know of any C++ functions that will take the average of an array, but it is very simple to write one. – Kattie.S Jun 28 '17 at 02:07

1 Answers1

0

Lets see if I can help... When it says "call a function" that may very well mean call a function that you wrote if the perfect solution doesn't already exist. The below code will show you my implementation of #4.

#include <iostream>

double AvgFunction(int numTemp, int* temp);

int main()
{
    int numTemp;
    std::cout << "Please input the number of temperatures to be read" << std::endl;
    std::cin >> numTemp;
    int temp[numTemp];
    for (int i = 0;i < numTemp; i++)
    {
        std::cout << "Please input temperatures for day "<< (i+1) << std::endl;
        std::cin >> temp[i];
    }
    std::cout << AvgFunction(numTemp, temp) << std::endl;

    return 0;
}

double AvgFunction(int numTemp, int* temp)
{
    double returnVal = 0;
    for(int i = 0; i < numTemp; i++)
    {
        returnVal += temp[i];
    }
    returnVal /= numTemp;
    return returnVal;
}

The function I call, AvgFunction, is written below main. This link explains functions and may be the most useful in learning how to be successful in problems like this: http://www.cplusplus.com/doc/tutorial/functions/

To complete #3 you will have to write another function. Use the same structure, but different content within to solve the problem. An explanation of the simplest sorting algorithm: http://www.geeksforgeeks.org/selection-sort/

The operators I use in the function are called compound operators. More info on that if you search "compound operators."

The line I added before main is a function prototype. More info on that if you search "function prototype."

Hope this helps

  • like Stargatur use a vector instead of VLA [here](https://stackoverflow.com/a/1887178/7868736) is why – Manvir Jun 28 '17 at 03:41