functions and arrays to calculate gpa - c++

[![enter image description here][1]][1]
This is what I have tried out but whenever I run the program it crashes and says error, although it compiles correctly. It asks me to enter department number but after that doesn't show me any output
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
const int STDTs = 25;
const int DEPTs = 7;
void initializeGPAs(double gpa[][DEPTs])
{
for(int i=0;i<STDTs;i++)
for(int j=0;j<DEPTs;j++)
gpa[i][j]=(10+rand()%31)/10;
}
void computeDeptAvg(double gpa[][DEPTs] , double deptAvg[])
{
for(int i=0;i<STDTs;i++)
{
int sum=0;
for(int j=0;j<DEPTs;j++)
sum+=gpa[j][i];
deptAvg[i]=double(sum/STDTs);
}
}
int StdsOnProbationCount(double gpa[][DEPTs])
{
int ctr=0;
for(int i=0;i<STDTs;i++)
for(int j=0;j<DEPTs;j++)
if(gpa[i][j]<2)
ctr++;
return ctr;
}
int StdsOnProbationCountinDeptX(double gpa[][DEPTs], int x)
{
int ctr=0;
for(int i=0;i<STDTs;i++)
if(gpa[i][x-1]<2)
ctr++;
return ctr;
}
void showReport(double gpa[][DEPTs],string dept_names[], double deptAvg[], int ctr1, int ctr2, int x)
{
cout<<endl;
for(int i=0;i<DEPTs;i++)
{
cout<<'\t'<<dept_names[i]<<" ";
}
cout<<endl;
for(int i=0;i<STDTs;i++)
{
cout<<"Student "<<i+1<<": ";
for(int j=0;j<DEPTs;j++)
cout<<gpa[i][j]<<" ";
cout<<endl;
}
cout<<endl;
for(int t=0;t<DEPTs;t++)
cout<<"Dept Avg.: "<<deptAvg[t]<<" ";
cout<<endl<<endl;
cout<<"Total number of students who are on probation is: "<<ctr1;
cout<<endl;
cout<<"Number of students who are on probation in "<<dept_names[x-1]<<" Dept. is "<<ctr2;
}
int main()
{
double gpa[STDTs][DEPTs];
int ctr1, ctr2, x;
double deptAvg[DEPTs];
string dept_names[DEPTs]={"MATH","STAT","COMP","PHYS","CHEM","BIOL","GEOL"};
initializeGPAs(gpa);
computeDeptAvg(gpa, deptAvg);
ctr1 = StdsOnProbationCount(gpa);
cout<<"Enter Department Number [1 to 7]: ";
cin >> x;
ctr2 = StdsOnProbationCountinDeptX(gpa, x);
showReport(gpa, dept_names, deptAvg, ctr1, ctr2, x);
return 0;
}
edit: I figured everything out but my problem seems to be in this function because it displays zeroes for all the department averages
void computeDeptAvg(double gpa[][DEPTs] , double deptAvg[])
{
for(int i=0;i<STDTs;i++)
{
int sum=0;
for(int j=0;j<DEPTs;j++)
{
sum+=gpa[j][i];
deptAvg[j]=double(sum/STDTs*1.0);
}
}
}

I'll consider only your computeDeptAvg function.
Your sum variable should be of type double, declaring it as an int makes all of your calculations (specifically the sum+=gpa[j][i] and sum/STDTs, which is an integer division) prone to truncating errors.
You nested the two loops in the wrong order: You should sum the scores of all the students in a particular department in order to calculate the average of that department. Besides, deptAvg[j] is overwritten every time the outer loop is executed.
You have declared gpa in main as a double gpa[STDTs][DEPTs];, but in your loops you are using gpa[j][i], where j is in the range [0, DEPTs) and i in [0, STDTs).
A modified version could be:
void computeDeptAvg(double gpa[][DEPTs] , double deptAvg[])
{
for(int d = 0; d < DEPTs; d++)
{
double sum = 0.0;
for(int s = 0; s < STDTs; s++)
{
sum += gpa[s][d];
}
deptAvg[d] = sum / STDTs;
}
}

Related

Pick a number say 3 so you can enter 3 numbers then substract the maximum from the min

I tried changing a lot of integers and stuff but it didn't work and it gives me a random number like 53289432 even tho lets say i put in 3:5,1,2 it should output 3 since 5-2 is 3.
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int x[n];
int mn;
int mx;
for(int i=1;i<n;i++)
{
cin>>x[i];
for(int j=1;j<n;j++)
{
if(x[i]>x[j]);
}
{
x[i]=mn;
}
}
for(int i=1;i<n;i++)
{
for(int j=1;j<n;j++)
{
if(x[i]<x[j]);
}
{
x[i]=mx;
}
}
cout<<mx-mn;
}
You don't need an array:
int smallest = 0;
int largest = 0;
std::cout << "Enter quantity: ";
int quantity;
std::cin >> quantity;
if (quantity < 1)
{
std::cerr << "Invalid quantity.\n";
return 1;
}
std::cout << "Enter number: ";
std::cin >> smallest;
largest = smallest;
for (int i = 1; i < quantity; ++i)
{
std::cout << "Enter number: ";
int number;
std::cin >> number;
if (number < smallest) smallest = number;
if (number > largest) largest = number;
}
std::cout << "maximum from minimum: " << (smallest - largest) << "\n";
std::cout << "minimum from maximum: " << (largest - smallest) << "\n";
The above code uses a running minimum/maximum, so no arrays are needed.
No need for variable length arrays or having to figure out the array capacity at compile time.
Your code carries several holes,
Firstly,
for(int i=1;i<n;i++)
{
cin>>x[i];
This won't take n integers, it will only take (n-1) integers as input. You need to initialise i=0, for n integers;
Secondly,
for(int i=1;i<n;i++)
{
cin>>x[i];
for(int j=1;j<n;j++)
{
if(x[i]>x[j]); //This will compare with garbage when i=1 and j>1
}
{
x[i]=mn;
}
}
Your comparison, will only be valid for first iteration of i=1 and j=1, after j>1, it will pick garbage value, since, you haven't taken any input yet.
It is suggested, to first take all the inputs, then do the comparison, or other operations.
Here is my solution
I think this is what you are trying to do!
#include <iostream>
#include <climits>
using namespace std;
int main()
{
int n;
cin>>n;
int x[n];
int mn;
int mx;
//First take the inputs in array x
for(int i=0;i<n;i++)
{
cin>>x[i];
}
//Find maximum and store it in mx
mx = INT_MIN; //This stores minimum in mx variable (climits)
for(int j=0;j<n;j++)
{
if(mx<x[j])
mx=x[j];
}
//Find minimum and store it in mn
mn = INT_MAX; //This stores maximum in mn variable (climits)
for(int j=0;j<n;j++)
{
if(mn>x[j])
mn=x[j];
}
int ans = mx - mn;
cout<<ans<<endl;
}
There is a better solution where you don't use extra space(array), and by using only 2 variables, you can find the difference. But I would recommend you to first understand this concept, and take a look, how array works, before moving towards any optimised solution.

Retrieving and displaying even number

So I am trying to get my even numbers to appear and tell me the result of the user input. Not sure why my function is an error in my main when I tried to just have it output in my even function won't print. Unsure why? also curious should I lock and make my array constant? It isn't changing but might feel like I have to make it constant and lock it in case if I have to change something to check for an even number. Plus I am a beginner and barely learning about arrays.
#include <iostream>
#include <string>
using namespace std;
// putting our voids first to declare be declare and run first
void fillUp(int num[], int size)
{
//user input
cout<<"Enter "<<size<<" and then press the ENTER/RETRUN: ";
for(int i = 0; i < size; i++)
{
cin>> num[i];
}
}
//obtaining user input and totaling them up
int total(int num[], int size)
{
int total = 0;
for(int i = 0; i < size;i++)
{
total += num[i];
}
return total;
}
int EvenElements(int num[], int size, int& evenCounter)
{
for(int i = 0; i<size;i++)
{
if(num[i] % 2 == 0)
{
evenCounter++;
}
}
return(evenCounter);
}
//display the array in a row
void display(int num[], int size)
{
for(int i = 0; i<size;i++)
{
cout<<num[i]<<" ";
}
}
//main function to display the results of the user input
int main()
{
int numOne[4], numTwo[5], numThree[6], evenCounter;
fillUp(numOne, 4);
fillUp(numTwo, 5);
fillUp(numThree, 6);
cout<<"The numbers in the array are: ";
display(numOne, 4);
cout<<" and the total of these numbers is "<<total(numOne, 4)
<<endl;
cout<<"The numbers in the array are: ";
display(numTwo, 5);
cout<<" and the total of these numbers is "<<total(numTwo, 5)
<<endl;
cout<<"The numbers in the array are: ";
display(numThree, 6);
cout<<" and the total of these numbers is "<<total(numThree, 6)
<<endl;
cout<<"This is how many evens were in the array: ";
EvenElements(evenCounter);
}
Your EvenElements() requires three arguments and you're passing only one and therefore, your code as it is will not compile. Try the following (read the comments for the last 3 lines):
#include <iostream>
#include <string>
using namespace std;
// putting our voids first to declare be declare and run first
void fillUp(int num[], int size)
{
//user input
cout<<"Enter "<<size<<" and then press the ENTER/RETRUN: ";
for(int i = 0; i < size; i++)
{
cin>> num[i];
}
}
//obtaining user input and totaling them up
int total(int num[], int size)
{
int total = 0;
for(int i = 0; i < size;i++)
{
total += num[i];
}
return total;
}
int EvenElements(int num[], int size, int& evenCounter)
{
for(int i = 0; i<size;i++)
{
if(num[i] % 2 == 0)
{
evenCounter++;
}
}
return(evenCounter);
}
//display the array in a row
void display(int num[], int size)
{
for(int i = 0; i<size;i++)
{
cout<<num[i]<<" ";
}
}
//main function to display the results of the user input
int main()
{
int numOne[4], numTwo[5], numThree[6], evenCounter;
fillUp(numOne, 4);
fillUp(numTwo, 5);
fillUp(numThree, 6);
cout<<"The numbers in the array are: ";
display(numOne, 4);
cout<<" and the total of these numbers is "<<total(numOne, 4)
<<endl;
cout<<"The numbers in the array are: ";
display(numTwo, 5);
cout<<" and the total of these numbers is "<<total(numTwo, 5)
<<endl;
cout<<"The numbers in the array are: ";
display(numThree, 6);
cout<<" and the total of these numbers is "<<total(numThree, 6)
<<endl;
cout<<"This is how many evens were in the array: ";
// Make sure you initialise evenCounter
evenCounter = 0;
// Call EventElement with the right parameters
EvenElements(numThree, 6, evenCounter);
// Display the result
cout<<evenCounter<<endl;
}
Note: Consider using macros for your sizes
#define SIZE_1 4
#define SIZE_2 5
#define SIZE_3 6
and then replace the hard-coded numbers with the appropriate sizes.

My Bubblesort won't sort anything, and won't let me pass by reference

For my CS class we are making a program that reads in numbers, calculates the inflation rate from those 2 numbers, sends those numbers to an array, bubblesorts the array, and then prints that array out after it is sorted. However, I can't get my bubblesort to sort, either there is a compiler error when I use &, or there it just doesnt sort. Can anyone help? Thank you!
#include
using namespace std;
double InflationRate(float old_cpi, float new_cpi);
double inflateRate;
void getCPIValues(float &old_cpi, float &new_cpi);
float swap_values(float&i, float&j);
void bubbleSort(float a[], int number_used);
float old_cpi=-1;
float new_cpi=-1;
const int MAX_RATES=20;
float a[MAX_RATES]={};
int main() //C++ programs start by executing the function main
{
char Continue = 'y';
double total = 0;
int i=0;
do{
getCPIValues(old_cpi, new_cpi);
InflationRate(old_cpi, new_cpi);
cout<< "Inflation rate is "<<inflateRate<<endl;
total = total+inflateRate;
cout << "Try again? (y or Y): ";
i++;
a[i-1]= inflateRate;
cin >> Continue;
}while ((Continue == 'y')||(Continue == 'Y'));
cout<<"Average rate is "<<total/i<<endl;
int number_used= i-1;
for (int p; p<=number_used; p++)
{
cout<<a[p]<<endl;
}
return 0;
}
double InflationRate(float old_cpi, float new_cpi)
{
inflateRate = (new_cpi - old_cpi)/old_cpi*100;
return(inflateRate);
}
void getCPIValues(float &old_cpi, float &new_cpi)
{
cout<<"Enter the old and new consumer price indices: ";
cin>>old_cpi>>new_cpi;
if ((old_cpi<=0)||(new_cpi<=0))
{
do
{
cout<<"Error: CPI values must be greater than 0";
cin>>old_cpi>>new_cpi;
}while ((old_cpi<=0)||(new_cpi<=0));
}
}
float swap_values(float&i, float&j)
{
int temp = i;
i=j;
j=temp;
return(i, j);
}
void bubbleSort(float a[], int number_used)
{
for (int m = 0; m < number_used-1; m++)
for (int n = 0; n < number_used-m-1; n++)
if (a[n] > a[n+1])
swap_values(&a[n], &a[n+1]);
}

Trying to create a program that reads an array and prints the prime numbers

This is what I wrote so far, where did I go wrong?
#include <iostream>
using namespace std;
int main()
{
int x[5], count=0;
for (int i=0;i<5;i++)
cin>>x[i];
for (int i=0;i<5;i++)
{
for (int n=2;n<x[i];n++)
{
if (x[i]%n==0)
count++;
}
if (count==1)
cout<<x[i]<<" ";
}
}
Edit:
Many thanks to everyone that tried to help. The problem was that I had to int count in the loop so that it would start from 0 every time. Here's my new working code:
#include <iostream>
using namespace std;
int main()
{
int x[5];
for (int i=0;i<5;i++)
cin>>x[i];
for (int i=0;i<5;i++)
{ int count=0;
for (int n=2;n<=x[i];n++)
{
if (x[i]%n==0)
count++;
}
if (count==1)
cout<<x[i];
}
}
#include <iostream>
using namespace std;
int main()
{
int x[5], count=0;
for (int i=0;i<5;i++)
cin>>x[i];
for (int i=0;i<5;i++)
{
bool check = true;
for (int n=2;n<x[i];n++)
{
if (x[i]%n==0)
{
check = false;
break;
}
}
if (check)
cout<<x[i]<<" ";
}
}
For better complexity, change your nested for loop to :
for (int n=2;n<=sqrt(x[i]);n++)
Other way :
int main ()
{
for (int i=2; i<100; i++)
{
bool check=true;
for (int n=2; n*n<=x[i]; n++)
{
if (x[i] % n == 0)
{
check=false;
break;
}
}
if(check) cout << x[i] << " ";
}
return 0;
}
Here's my solution.
I basically assume all numbers are prime, but if I find them not to be prime I don't print them out.
int main() {
bool primeNumber;
int length = 0;
int x[5], count=0;
for (int i=0;i<5;i++){
length++;
cin>>x[i];
}
for (int i = 0; i < length; i++ )
{
primeNumber = true;
for (int j = 3; j <= x[i]/2; j += 2 )//Not necessary to check agains numbers higher than half the number you want to check
{
if (x[i] % j == 0)
{
primeNumber = false;
}
}
if(primeNumber){cout << "Primenumber: " << x[i] << endl;}
}
}
EDIT:
Here's a comment on your code
Let's say your array looks like this
{5,9,13,15, 17}
int main()
{
int x[5], count=0;
for (int i=0;i<5;i++)
cin>>x[i];
for (int i=0;i<5;i++)
{
for (int n=2;n<x[i];n++)
{
Here you loop from 2-4(2,3,4). It's only necessary to loop half the number you want to check. e.g. (x[i]/2), but you can do it like you have done here.
if (x[i]%n==0){
count++;}
When your number is 5 and you check agains n numbers This will produce 5%2=1,5%3=2,5%4=1. But 5 is a prime number and your code didn't manage to detect that.
}
if (count==1)
Let's say you did find that 5 is a prime above and you managed to increment count to 1. What happens when you find another prime number in your array e.g. 7. That will make count be equal to 2. But you only check against 1, so therefor your next prime number doesn't get printed because count will only be equal to 1 after the first prime number and not the rest.
cout<<x[i]<<" ";
}
}
Hopefully you understood my explanation. It always helps to go over your code by hand or debug mode and think what the code will do with different numbers.
All numers are divideable by itself. That will make all numbers prime if you try to divide numbers with themselves

Recursion function is not responding

# include <iostream>
using namespace std;
class mm
{
private:
int k[1000];
int n;
int i;
int a;
int b;
int f;
public:
mm ()
{
a=0;
b=1;
f=0;
i=0;
for(int i=0; i<n;i++)
k[i]=0;
};
~mm()
{
}
void fib(int n)
{
for (int i=0;i<n;i++)
{
if (i<=1)
f=i;
else
{
f=a+b;
a=b;
b=f;
}
k[i]=f;
}
for (int j=0;j<n;j++)
cout<<k[j]<<" ";
}
int se (int n, int i)
{
if (n==1)
return 1;
else
return 1/k[i] + se (n-1, i+1);
}
};
int main()
{
int n;
cout<<"Enter n:";
cin>>n;
mm p;
cout<<"fib: "<<endl;
p.fib(n);
cout<<endl;
cout<<"se: ";
cout<<p.se(n,0);
return 0;
}
Recursion function from main is not responding. Maybe the array k[i] is not working, but I cant find the reason. Can anyone help me?
k[0] is set to 0. When you then call se(n,0) in main, it computes 1/k[0] + se(n-1,1) which is a division by zero.