How do I find the average of the whole class? - c++

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
struct Students
{
char name[30];
char first_name[30];
int n1;
int n2;
int n3;
}student list[10];
int n;
void reading_student_list()
{
int i;
ifstream f("in.txt");
f>>n;
for(i=0;i<n;i++)
{
f>>student_list[i].name;
f>>student_list[i].first_name;
f>>student_list[i].n1;
f>>student_list[i].n2;
f>>student_list[i].n3;
}
f.close();
}
void showing_student_list()
{
int i;
ofstream g("out.txt");
g<<"\n Student list: ";
for(i=0;i<n;i++)
{
g<<"\n\n";
g<<student_list[i].name<<" ";
g<<student_list[i].first_name<<" ";
g<<student_list[i].n1<<" ";
g<<student_list[i].n2<<" ";
g<<student_list[i].n3<<" ";
g<<endl<<"Average: "<<float(student_list[i].n1+student_list[i].n2+student_list[i].n3)/3;
}
cout<<"\n Open file out.txt";
g.close();
}
int main()
{
reading_student_list();
showing_student_list();
}
I did the average for the grades per student, but I can't figure out how to find the average for the whole class.
After doing the average for the whole class, I can figure out the students that have their average below average of the class and the highest average of the class.

Here's how you would add up all the n1 values (whatever that means).
int total = 0; // start the total at zero
for (int i = 0; i < n; i++) // for each student
total += list[i].n1; // add list[i].n1 to total
Hopefully you can adapt that to suit your particular requirements.

Related

how to find summation of all elements of array c++

I'm new to c++ but long story short , i want to write a c++ program that will accept input from user through an array and it will sum each array element input
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int main() {
int array[100];
int sum;
for(int i=0; i<100; i++){
cout<<"Insert element "<<i<<": ";
cin>>array[i];
sum = array[i]+ //summ with the next array input;
cout<<sum;
}
return 0;
}
means that if i enter any integer the program should be able to give the summation of the inputs in sequence from the first input to the last input
C++ standard library already has a function to do this which is std::accumulate:
#include <iostream>
#include <numeric>
int main() {
int array[5] = {1,2,3,4,5};
int total = std::accumulate(std::begin(array), std::end(array), 0);
return 0;
}
If you plan to use not a full array you should use std::begin(array), std::begin(array) + amount as ranges.
initialize your sum var to 0 initially and write sum+=array[i] instead of what you have written, there is also a limitation in this program as value of sum after all user input should be <=10^9 as int datatype store no. approximately upto 10^9 so take note of this fact also. Write cout<<sum<<endl; instead to be able to distinguish till previous input sum to the new input sum.
Hope this will help.
You can use a do while loop:
#include<iostream>
#include<string>
#include<math.h>
int main()
{
int array[100];
int sum=0;
int i=0;
std::cout<<"Insert element"<<" "<<i<<": ";
std::cin>>array[i];
do
{
sum=sum+array[i];
std::cout<<sum<<std::endl;
i++;
std::cout<<"Insert element"<<" "<<i<<": ";
}while(std::cin>>array[i]);
return 0;
}
If all you want is the sum then just compute the sum. No need to store anything:
#include <iostream>
#include <string>
#include <math.h>
int main() {
int sum = 0;
for(int i=0; i<100; i++){
std::cout << "Insert element " << i << ": ";
int t;
std::cin >> t;
sum += t
std::cout << sum;
}
}

why does it shows error even though it seems to be quite appropriate?

#include <iostream>
using namespace std;
int factors(int);
int main() {
int n;
cout<<"Enter a number: ";
cin>>n;
factors(n);
return 0;
}
factors(int n){
//To find out factors
int i, arrA[5];
arrA[0]=1;
cout<<"Factors: ";
for(int i=1;i<=n;i++){
for(int j=2;j<=n;j++){
if(n%j==0){
arrA[i]=j;
}
else if(n%j!=0){
i--;
}
}
}
for(int z=0;z < (sizeof(arrA)/sizeof(arrA[0]));z++){
cout<<arrA[z]<<" ";
}
}
This is a c++ code and for finding the factors of a given number.
But the problem is it's not producing the desired output.Problem is with the processing part(I think). It shows only "factors: " and continues to process and gives an error stating - "Untitled.exe has stopped working". Please fix the error and provide an elaborate explanation.
Clearly the problem comes from the for(int i=1;i<=n;i++){ that contains i-- which is a risk for infinite loop.
Since you tagged c++, do it with c++ this avoids a static array (arrA[5] is a good thing if you are sure there is only 5 factors but it depends on your n)
Instead use a std::list
#include <iostream>
#include <list>
using namespace std;
void factors(int n, std::list<int> & factors);
int main() {
int n;
cout<<"Enter a number: ";
cin>>n;
std::list<int> fact;
factors(n, fact);
cout<<"Factors: ";
std::list<int>::iterator it=fact.begin();
for(;it!=fact.end();it++)
{
std::cout << *it<<", ";
}
return 0;
}
void factors(int n, std::list<int> & factors)
{
for(int j=2;j<=n;j++){
if(n%j==0){
factors.push_back(j);
}
}
}

C++ list with array of strings

I have to make a list with people and their exam result in C++. My problem is that I don't know how to input the array.
I was trying to make 3D array of strings, but it doesn't works. It must be in function! If you have a better suggestions, I will be very grateful
My input must be something like:
Peter Evens 4.86
*other people**
*average result of the group**
*the one with the heighest grade**
*the one with lowest grade**
This is what I have done for now:
#include <iostream>
#define MAXN 200
#define MAXM 200
#define MAXX 200
using namespace std;
void input(char list[][MAXN][MAXM], int n) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
for (int p = 0; p < n; p++)
cin >> list[i][j][p];
}
}
int main() {
char list[31][MAXN][MAXM];
int n;
cin >> n;
input(list, n);
return 0;
}
You don't need to run the third iteration since you don't have to store the input character by character. The third dimension can be used for storing the entire string.
Try the following way. Observe the corrections i have made in your snippet.
#include <iostream>
#define MAXN 200
#define MAXM 200
#define MAXX 200
using namespace std;
void input(char list[][3][MAXM], int n) {
for (int i = 0; i < n; i++) //number of entries
for (int j = 0; j < 3; j++) { //3 fields - fname, lname and marks
cin >> list[i][j];
}
}
int main() {
char list[31][3][MAXM];
int n;
cin >> n;
input(list, n);
return 0;
}
You must use a vector of struct.
Define your struct this way:
typedef struct details{
string Fname;
string Lname;
float marks;
} details;
And vector should look like
vector< details > info;
I am just showing you how to implement this in the following code:
void insertValues(vector< details >& info){
int n;
details d;
cout<<"Enter the size of record: ";
cin>>n;
cout<<"Enter records: "<<endl;
for(int i=0;i<n;i++){
cin>>d.Fname>>d.Lname>>d.marks;
info.push_back(d);
}
}
void LowestScorer(vector< details > info){
details d;
vector< details >::iterator it=info.begin();
d=*it;
for(;it!=info.end();it++){
if(d.marks > it->marks){
d=*it;
}
}
cout<<"Lowest Scorer: "<<d.Fname<<" "<<d.Lname<<" "<<d.marks<<endl;
}
main should look like this:
int main(){
vector< details > info;
insertValues(info);
LowestScorer(info);
return 0;
}
As mentioned in above comments, it's good time to learn vector. It's very easy. Below is a very simple and neat solution:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector<pair<string, string> > Container;
int main()
{
Container container;
container.push_back(make_pair("Peter Evens", "4.86"));
container.push_back(make_pair("Other name", "his score"));
return 0;
}

c++ Scrabble game using arrays

I have to make a scrabble scoring game using 2 arrays. The first array holds the user inputted word and the second array holds the value of each letter. Lastly a function is needed to calculate the score. Im having trouble assigning the user values to the second array to get the score and getting the right code for the function.
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <ctime>
using namespace std;
int scoreCalculator(int total);
char userWord;
int points;
int total=0;
int main()
{
char userWord[11];
for (int i=0; i<11; i++)
{
userWord[i]='\0';
}
cout<<"Enter your word less than 10 letters: "<<endl;
cin>>userWord;
cout<<"Here is the word you inputted: "<<userWord<<endl;
int scrabblePoints[26]={1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
for(int j=0; j<26; j++)
{
userWord[i]
}
cout<<"Here is your score: "<<scoreCalculator(total)<<endl;
}
int scoreCalculator(int total)
{
total+=scrabblePoints[j];
}
This is what I have so far and where im stuck at
#include <iostream>
int main()
{
std::string input;
std::cin>>input;
// fill this with scrable values
int scrable_values[26] = {1,3,3,2,1,4,2,4,1,9,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
int ans = 0;
for(int i=0;i<input.size();i++)
{
ans += scrable_values[input[i]-97];
}
return ans;
}

Find the biggest sum out of the all, it containing numbers and

I need to find the biggest sum and it containing numbers and whether the first or second number out of two was bigger. How to find it?
Let's say that n=10, the two put numbers are 6 and 2, followings: 7 and 1,
5 and 6, 1 and 8, 4 and 3. Then the answer should be that the biggest sum is 11, it containing numbers are 5 and 6, and the bigger numb was the second one.
I have a code:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int p, a, i;
int n;
int sum;
ofstream fr("Rezults.txt");
ifstream fd("Data.txt");
fd>>n;
cout<<n<<endl;
for (i=1; i<=n/2; i++)
{
fd>>p>>a;
sum=p+a;
for (int j=sum; j<=n/2; j++);
{
cout<<sum<<endl;
}
}
fd.close();
fr<<sum;
fr.close();
return 0;
}
I think your code should be like this :
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int p, a, i;
int n;
int sum;
ofstream fr("Rezults.txt");
ifstream fd("Data.txt");
fd>>n;
cout<<n<<endl;
fd>>p>>a;
int biggestSum=p+a;
int first = p;
int second = a;
for (i=2; i<=n/2; i++)
{
fd>>p>>a;
sum=p+a;
if(sum > biggestSum)
{
biggestSum = sum;
first = p;
second = a;
}
}
cout <<"biggest sum is "<<biggestSum<<"\n";
cout <<"The first number is "<<first<<"\n";
cout<<"The second number is "<<second<<"\n";
fd.close();
fr<<sum;
fr.close();
return 0;
}
updated : you should be careful to the index i of the for loop it should start by 2 since you read the first two numbers before the for loop.