Home > Mobile >  Is there anyway to make this code shorter, I just started learning C
Is there anyway to make this code shorter, I just started learning C

Time:06-23

#include <iostream>
using namespace std;

int main()
{
    double mark1, mark2, mark3, mark4, mark5, mark6, mark7, mark8, mark9, mark10, average;
    cout << "Input mark for learner 1";
    cin >> mark1;
    cout << "Input mark for learner 2";
    cin >> mark2;
    cout << "Input mark for learner 3";
    cin >> mark3;
    cout << "Input mark for learner 4";
    cin >> mark4;
    cout << "Input mark for learner 5";
    cin >> mark5;
    cout << "Input mark for learner 6";
    cin >> mark6;
    cout << "Input mark for learner 7";
    cin >> mark7;
    cout << "Input mark for learner 8";
    cin >> mark8;
    cout << "Input mark for learner 9";
    cin >> mark9;
    cout << "Input mark for learner 10";
    cin >> mark10;

    average = (mark1   mark2   mark3   mark4   mark5   mark6   mark7   mark8   mark9   mark10) / 10;
    cout << "The class average mark is:" << average << endl;
    return 0;
}

CodePudding user response:

Using an array and looping through its elements, you can use:

const int COUNT = 10;
double mark[COUNT];
double sum = 0;

for (int i = 0; i < COUNT; i  )
{
    cout << "Input mark for learner " << (i 1) << ": ";
    cin >> mark[i];
    sum  = mark[i];
}

double average = sum / COUNT;

In case you dont need the seperate marks later anywhere, you could just use a lokal mark inside the loop

const int COUNT = 10;
double sum = 0;

for (int i = 0; i < COUNT; i  )
{
    double mark;
    cout << "Input mark for learner " << (i 1) << ": ";
    cin >> mark;
    sum  = mark;
}

double average = sum / COUNT;

CodePudding user response:

Welcome to the community! First of all, it would be a good idea to use indentation for your code, so it is readable to others as well. If you just want to calculate the average, then you can do it with the code below. I tried to keep it as simple as possible for you.

#include <iostream>

using namespace std;

int main()

{
    int numberOfMarks = 10;
    double temp, average = 0;

    for (int i = 0; i < numberOfMarks;   i)
    {
        cout << "Input mark for learner " << i   1 << ": ";
        cin >> temp;
        average  = temp;
    }

    average = average / numberOfMarks;
    cout << endl << "The class average mark is:" << average << endl;
    return 0;
}

CodePudding user response:

You can use a number of standard library functions, giving you an opportunity to learn them at the same time.

#include <algorithm>
#include <array>
#include <iostream>
#include <numeric>

int main() {
  std::array<double,10> mark; // use std::vector if the number of entries is variable
  std::for_each(begin(mark), end(mark), [i = 0](auto& m) mutable {
    std::cout << "Input mark for learner " <<   i << "\n";
    std::cin >> m;
  });
  std::cout << std::reduce(begin(mark), end(mark)) << "\n";
}

Here is documentation for you to read:

Also avoid using namespace std;.

  • Related