CCAV: SOLD
Hi, I would like to thank everyone who has purchased some of my #NFTs recently. #eCash $XEC pic.twitter.com/Xxil6H6kbo
— Gaexe (@gaexe_) December 13, 2024
OMG my sultan is comeback #eCash $XEC #NFTCollection pic.twitter.com/eJhVgY80e8
— Gaexe (@gaexe_) December 20, 2024
Hello, young scholars! For those of you currently in your first semester and perhaps still unsure or confused about how to write a C++ program to calculate the average value, let me help explain.
Step 1: Understanding the Formula
Before diving into programming, you must first know the formula for calculating the average. In case anyone needs a refresher, the formula is the sum of all values divided by the number of values.
For example, if there are 5 students in a class, 3 of them scored 10, and the other 2 scored 5, the class average would be calculated as 40/5 = 8.
Step 2: Defining Variables
Based on the formula, we need the following variables:
- To store individual sample values.
- To store the total sum of all values.
- To store the total number of samples.
- To store the final average result.
Step 3: Writing the Program
Below is a simple program example:
- The variable
angka
is used to hold the sample value. - The variable
total
holds the cumulative sum of the values. - The variable
rata
stores the result of the average calculation. - The variable
jumlah
represents the total number of samples.
We first prompt the user to input the desired number of samples. Then, we use a loop to process each input until all samples are collected.
Simple Average Calculation
#include <iostream.h>
#include <conio.h>
/**
* Simple Average Calculation
*/
void main() {
float angka, total = 0, rata;
int jumlah;
cout << "1. Calculate Average (fixed number of inputs)" << endl;
cout << endl;
cout << "Enter the number of values: "; cin >> jumlah;
cout << endl;
for (int i = 1; i <= jumlah; i++) {
cout << "Enter value: "; cin >> angka;
total = total + angka;
}
cout << endl;
cout << "Total: " << total << endl;
rata = total / jumlah;
cout << "Average: " << rata;
getch();
}
If you have any questions, feel free to ask in the comments section below.
Average Calculation with Flexible Input
This example explains how to calculate the average when the user can input as many values as they want. The input continues until the user decides to stop by choosing "n" (No). The program will still notify the user clearly.
#include <iostream.h>
#include <conio.h>
/**
* Flexible Input Average Calculation
*/
void main() {
int i = 1;
float angka, total = 0, rata;
char jwb;
cout << "1. Calculate Average (flexible input)" << endl;
cout << endl;
do {
cout << "Enter value: "; cin >> angka;
total = total + angka;
i++;
cout << "Add more data? (y/n): "; cin >> jwb;
} while (jwb == 'y');
cout << endl;
cout << "Total: " << total << endl;
rata = total / i;
cout << "Average: " << rata;
getch();
}
I hope this explanation helps. Happy learning! 😊
Comments and Responses
Comment 1
ZEN BOXS - July 16, 2014, 03:21
"Is that an array method, bro?"
Response 1
"No, bro, it's just regular data types and variables. If you want to learn about arrays, feel free to use the search box above to find all posts related to arrays."
Comment 2
ZAKMI ZANK - October 14, 2015, 00:09
"What’s the purpose of (int i = 1; i <= jumlah; i++)
? Still learning 😃"
ANONYMOUS - January 14, 2016, 00:01
"It didn’t work, bro."
RICKY MANDALA - January 18, 2016, 08:24
"Bro, I also had issues with the for
loop even though I copied it exactly as shown above."
GHIFARY MAULANA FANONY - March 19, 2016, 13:01
"Where’s the source code, bro?"
BAYU HERLAMBANG - February 28, 2017, 20:36
"It didn’t work in the compiler using CMD. Fatal error: iostream.h; no such file or directory."
BAYU HERLAMBANG - March 1, 2017, 19:37
"I’m using command prompt. If iostream.h doesn’t work, what should I use instead, bro?"
MUHAMMAD FARIS HIDAYATULLAH - December 24, 2016, 16:43
"Bro, it didn’t work for me. Could you help? Here’s my number: 0823-0170-3615."
GALIH MAEY - March 8, 2017, 22:43
"I’m using DEVC++. I copied the code, but it didn’t work. Any advice?"
FUJI BARKAH - April 9, 2017, 16:29
"Where does the average formula come from? You didn’t explain it."
DESTIA DESCULL - November 7, 2017, 10:20
"Just to add, in some cases, compilation errors can occur because the library isn’t detected. In DEVC++, you need to include <iostream>
without the .h
and add using namespace std;
🙂"
KEVIN QUDRATA - January 9, 2018, 16:20
"Still confused."
NUR HASANAH - April 19, 2018, 12:14
"I don’t understand where the inputs like 'Enter 5', 'Enter 10', and so on come from. What if the input is 'Enter 3'? Is it the same?"
NUR HASANAH - April 19, 2018, 12:24
"For example, if I enter the number 3:Enter value: 10
Enter value: 10
Enter value: 10
Total: 30
Average: 10
Is that correct?"
REGA SATYA PUTRA - June 8, 2018, 12:09
"How can I calculate GPA, bro? I want to display student data (like NIM) first. How do I do that?"
Response 2
"Use a for
loop parameter, bro. It’s like this in a logical expression: i starts at 1, and as long as i is less than or equal to jumlah, increment i by 1.
This ensures that i continues increasing until it reaches the value specified by the user for jumlah. Good luck!"
Response 3
"What compiler are you using? If you copied the code directly into the editor/compiler, there might be spaces causing errors. Try manually replacing spaces using your keyboard instead of pasting the code."
Response 4
"You might be using an old compiler, probably from the 1990s. Try modifying <iostream.h>
to just <iostream>
."
Comment 3
Commenter:
"How can I make the inputs appear in order, like 'Enter value 1', 'Enter value 2', and so on?"
Response 3
"Use a sorting function like the one below:"
C++ Sorting Example
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
int n = sizeof(arr) / sizeof(arr[0]);
// Sort the array
sort(arr, arr + n);
cout << "\nArray after sorting using default sort is:\n";
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
return 0;
}
Output
Array after sorting using default sort is:
0 1 2 3 4 5 6 7 8 9