Data cleanup array - c++

I need to write a program that cleans up two arrays with 12 scores and gets rid of the "bonus scores" (where the denominator is 0). I've looked at similar problems, but I'm at a point in which there's three main problems that either no one seems to have or haven't been addressed. the first one is when declaring the new (not bonus) arrays in main it requires that I enter a constant value, however; the size of the arrays must change for the clean_data function to work.
the second one is when calling clean_data in main it marks "deno" and "num" as an error saying that "argument of type int * is incompatible with parameter of type int**", and the last one says that the function clean_data cannot convert argument 1 from int[12] to int*[].
I've been poking at this code for 3 days and looked at my professor's notes and textbook and can't find a way to fix it. please help.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
//prototypes for functions
void read_data(int nume[], int deno[], int size);
void report_data(int nume[], int deno[], int size);
void report_overall_grade(int nume[], int deno[], int size);
//Data cleanup
void clean_data(int *nume[], int *deno[], int size, int *notBonusNum[], int *notBonusDen[], int &newSize);
int main()
{
//declare constat, arrays, and variables;
const int NUM_GRADES = 12;
int nume[NUM_GRADES];
int deno[NUM_GRADES];
int newSize = 12;
int notBonusNum[newSize];
int notBonusDen[newSize];
//call functions
read_data(nume, deno, 12);
cout << "Original scores: " << endl;
cout << fixed << showpoint << setprecision(1);
report_data(nume, deno, 12);
system("pause");
system("cls");
report_overall_grade(nume, deno, 12);
//Data cleanup
clean_data(nume, deno, 12, notBonusNum, notBonusDen, newSize);
cout << "There are " << newSize << " scores that aren't bonuses:" << endl;
report_data(notBonusNum, notBonusDen, newSize);
system("pause");
return 0;
}
//function definitions
void read_data(int nume[], int deno[], int size)
{
//open file
ifstream inputFile;
inputFile.open("scores.txt");
if (inputFile.fail())
{
cout << "File does not exist." << endl;
system("pause");
exit(1);
}
//read data
int count;
for (count = 0; count < size; count++)
{
inputFile >> nume[count];
inputFile >> deno[count];
}
inputFile.close();
}
void report_data(int nume[], int deno[], int size)
{
int count;
int number = 1;
for (count = 0; count < size; count++)
{
if (deno[count] > 0)
{
int numerator = nume[count];
int denominator = deno[count];
double percent = (double)numerator / denominator * 100;
cout << "Score " << number << " is " << numerator << " / " << denominator << " = " << percent << "%." << endl;
}
else
{
cout << "Score " << number << " is " << nume[count] << " / " << deno[count] << " = Bonus Points!" << endl;
}
number++;
}
}
void report_overall_grade(int nume[], int deno[], int size)
{
int count;
int totalNum = 0;
int totalDeno = 0;
double overallGrade;
for (count = 0; count < size; count++)
{
totalNum += nume[count];
totalDeno += deno[count];
}
cout << "Total points earned: " << totalNum << endl;
cout << "Total points possible: " << totalDeno << endl;
overallGrade = (double)totalNum / totalDeno * 100;
cout << "Overall grade: " << overallGrade << endl;
}
//Data cleanup
void clean_data(int *nume[], int *deno[], int size, int *notBonusNum[], int *notBonusDen[], int &newSize)
{
int count;
for (count = 0; count < size; count++)
{
int count2 = 0;
if (deno[count] != 0)
{
notBonusNum[count2] = nume[count];
notBonusDen[count2] = deno[count];
newSize--;
count2++;
}
}
}

Related

Creating program that takes 5 grades from the user and finds the lowest grade, and then outputs average grade after dropping the lowest grade entered

`
#include <iostream>
#include <iomanip>
using namespace std;
void getGrades(double g[], const int SIZE)
{
cout << "Please enter " << SIZE << " grades:" << endl;
for(int i = 0; i < SIZE; i++)
{
cin >> g[i];
}
}
double getAverage(double g[], const int SIZE)
{
int total = 0;
for(int i = 0; i < SIZE; i++)
{
total += g[i];
}
return total/SIZE;
}
void findDropInfo(double g[], const int SIZE, int &lowest, double average)
{
int total = 0;
lowest = g[0];
for(int i = 1; i < SIZE; i++)
{
if (lowest > g[i]) {
lowest = g[i];
}
}
average = (total - lowest)/SIZE;
return average;
}
void printData(double g[], int lowest, double average, double avg_before)
{
cout << "The 5 grades entered by the user are:" << endl;
cout << g[];
cout << "Grade dropped: " << lowest << endl;
cout << "Final Average: " << average << endl;
cout << "Average before drop: " << avg_before << endl;
}
// TODO: Complete the function definitions
int main()
{
const int SIZE = 5;
double grades[SIZE];
int lowest;
double avg,
avgBeforeDrop;
// TODO: Add function calls
getGrades(grades[SIZE], SIZE);
getAverage(grades[SIZE], SIZE);
findDropInfo(grades[SIZE], SIZE, lowest, avg);
printData(grades[SIZE], lowest, avg, avgBeforeDrop);
return 0;
}
`
Whenever I run the program, I get multiple errors saying there's no matching candidate function. I'm not sure if the problems are in the functions themselves or in the function calls, but from what I know the functions themselves should be fine. I'm also told there's an expected expression in g[] but I' not sure what's wrong there either, as it's meant to be empty.
Most issues have already been resolved in the comments, but note: cout << g[] does not print the elements of g.
The way to do this is
char separator = /* the character you want to use to separate the printed elements of g */
for (int i = 0; i < SIZE; i++)
{
cout << g[i] << separator;
}
if (separator != '\n') cout << '\n'; //this is to put the next print on the next line
I would put this as a comment but I don't have enough reputation :|

Why is my txt file not opening successfully? c++

I am trying to open the txt file I made for the rest of my code to successfully run. I am using c++ and xcode is the environment. When I run my code, I get this:
error in opening studentScore8.txt
And below that is the correct formatting for the table but weird numbers are there. I am confident that the location for the file is correct, and I am not seeing any issues in my code besides
printGrade(oneScore, average);
in the main function, which xcode is saying that the parameters oneScore and average are not initialized, but I thought I already did that above.
Does anyone know what the issue could be? This is a lab for my comp sci class, and my teacher doesn't seem to know what the issue is either. I am thinking it has something to do with xcode or my computer itself, but I am not sure. The entire code is below.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
#define inFile "/Users/spacecowboy/Desktop/cis/cis/lab 8/studentScore8.txt"
const int MAX_SIZE = 4;
void readStuData (ifstream& rss, int scores[], int id[], int& count, bool& tooMany);
float mean(int scores[], int count);
void printTable(int scores[], int id[], int count);
void printGrade(int oneScore, float average);
int main() {
ifstream rss;
// void printTable(scores, id, count);
// float mean(int scores, int count);
float average;
bool tooMany;
int scores[MAX_SIZE];
int oneScore;
int id[MAX_SIZE];
int count;
rss.open("studentScore8.txt");
if (rss.fail()){
cout << "error in opening studentScore8.txt" << endl;
}
readStuData(rss, scores, id, count, tooMany);
mean(scores, count);
printGrade(oneScore, average);
printTable(scores, id, count);
rss.close();
return 0;
}
void readStuData(ifstream& rss, int scores[], int id[], int& count,bool& tooMany){
int stuID;
int stuScore;
int i;
for (i = 0; i < MAX_SIZE; ++i) {
rss >> stuID;
rss >> stuScore;
id[i] = stuID;
scores[i] = stuScore;
++count;
}
if (count > MAX_SIZE){
tooMany = true;
}
else {
tooMany = false;
}
}
float mean(int scores[], int count)
{
float sum = 0;
int i;
for (i = 0; i < count; ++i) {
sum = sum + scores[i];
}
return sum / count;
}
void printGrade(int oneScore, float average){
ifstream rss;
rss >> oneScore;
if (((oneScore >= average -10) && (oneScore <= average + 10))) {
cout << "Satisfactory";
}
else if (oneScore == average + 10){
cout << "Satisfactory";
}
else if (oneScore > average + 10){
cout << "Outstanding";
}
else {
cout << "Unsatisfactory";
}
}
void printTable(int scores[], int id[], int count){
int i;
cout << endl;
cout << setw(10) << left << "Student ID" << "|";
cout << setw(6) << right << "Score" << "|" ;
cout << setw(14) << left << "Grade" << endl;
cout << setfill('-') << setw(30) << "" << endl;
for(i = 0; i < MAX_SIZE; ++i){
cout << left << setw(10) << id[i] << "|";
cout << left << " ";
cout << setw(2) << scores[i];
cout << right << " " << "|";
printGrade(scores[i], mean(scores, count));
cout << endl;
}
cout<< setfill('-') << setw(30) << "" << endl;
}

C++ Average Calculation Function Returning 0 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm writing a program that calculates the batting averages of a user defined number of players. The program displays the name of a player, their number of times at bat, number of hits, and their batting average. Finally, it displays the total number of times the players were at bat, the total number of hits, and the overall average. For some reason, the functions that calculate the individual player average and overall average are returning 0. It's probably something small, but I'm stumped as to how to try and fix it.
//Batting Average Calculator
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//Create Structure
struct Record
{
string name;
int AB;
int hit;
double avg;
};
int getSize(int);
void getData(Record[], int );
int calculateTotalAB(Record[], int, int);
int calculateTotalHit(Record[], int, int);
double calculateTotalAvg(Record[], int, double);
void calculateAvg(Record[], int);
void display(Record[], int, int , int, double);
int main()
{
const int MaxSize = 50;
Record players[MaxSize];
int size = 0;
int totalAB = 0;
int totalHit = 0;
double totalAvg = 0;
size = getSize(size);
getData(players, size);
totalAB = calculateTotalAB(players, size, totalAB);
totalHit = calculateTotalHit(players, size, totalHit);
calculateAvg(players,size);
totalAvg = calculateTotalAvg(players, size, totalAvg);
display(players, size, totalHit, totalAB, totalAvg);
}
//get number of players to be calculated
int getSize(int size)
{
cout << "Please enter the number of players on the team: ";
cin >> size;
return size;
}
//get Player name, AB, and hit
void getData(Record players[], int size)
{
string dummy;
getline(cin, dummy);
for (int i = 0; i < size; i++)
{
cout << "Please input the name of student " << i + 1 << ": ";
getline(cin, players[i].name);
cout << "Please input the number of times "<< players[i].name << " was at bat: ";
cin >> players[i].AB;
cout << "Please input the number of hits for " << players[i].name << ": ";
cin >> players[i].hit;
cout << " " << endl;
getline(cin, dummy);
}
}
int calculateTotalAB(Record players[], int size, int totalAB)
{
for (int i = 0; i < size; i++)
{
totalAB = totalAB + players[i].AB;
}
return totalAB;
}
int calculateTotalHit(Record players[], int size, int totalHit)
{
for (int i = 0; i < size; i++)
{
totalHit = totalHit + players[i].hit;
}
return totalHit;
}
void calculateAvg(Record players[], int size)
{
for (int i = 0; i < size; i++)
{
players[i].avg = players[i].hit / players[i].AB;
}
}
double calculateTotalAvg(Record players[], int size, double totalAvg)
{
double j = 0;
for (int i = 0; i < size; i++)
{
j = j + players[i].avg;
}
totalAvg = j / size;
return totalAvg;
}
void display(Record players[], int size, int totalHit, int totalAB, double totalAvg)
{
cout << fixed << showpoint << setprecision(3);
cout << "Player AB Hit Avg" << endl;
cout << " " << endl;
for (int i = 0; i < size; i++)
{
cout << players[i].name << setw(8) << players[i].AB << setw(5) << players[i].hit << setw(5) << players[i].avg;
}
cout << " " << endl;
cout << "Totals " << totalAB << " " << totalHit << " " << totalAvg << endl;
}
You are dividing an int by an int, which is calculated as an int, and storing it in a double. What you have to do is explicitly cast at least one of your int values to a double first, like this:
void calculateAvg(Record players[], int size)
{
for (int i = 0; i < size; i++)
{
players[i].avg = players[i].hit / (double) players[i].AB;
}
}

Input Validation: My Program cannot accept negative numbers but it is accepting them and I'm really confused on what to do

#include <iostream>
#include <iomanip>
using namespace std;
// Function Prototype
void sortArray(int array[], int size);
void showArray(const int arr[], int size);
void average(int testScores[], int size);
int main()
{
int *testScores;
int numGrades,count;
cout << "How many grades? " << endl;
cin >> numGrades;
testScores = new int[numGrades];
cout << "Please enter the scores below." << endl;
for (count = 0; count < numGrades; count++)
{
cin >> testScores[count];
}
sortArray(testScores, numGrades);
showArray(testScores, numGrades);
average(testScores, numGrades);
delete[] testScores;
testScores = 0;
system("pause");
return (0);
}
//function for ascending order
int * testScores[];
void sortArray(int array[], int size)
{
bool swap;
int temp;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (array[count]> array[count + 1])
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
// display array function
void showArray(const int arr[], int size)
{
cout << " Scores in ascending order." << endl;
for (int count = 0; count < size; count++)
{
cout << " " << arr[count] << "";
}
cout << endl;
cout << endl;
}
// function to get average of the array
void average(int testScores[], int numGrades)
{
float total = 0.0;
for (int count = 0; count < numGrades; count++)
{
total += testScores[count];
}
float average = total / numGrades;
cout << " This is the average of the scores entered." << endl;
cout << endl;
cout << fixed << showpoint << setprecision(2);
cout << " *** " << average << " ***" << endl;
}
So this program was made to allow students to enter as many test scores as they want and the program will show them in ascending order and then will calculate the average of all test scores. It works wonderful until you enter a negative test score and then it throws the averaging process off. I can't seem to be able to make the program not accept the negative numbers. Any clues or hints would work wonders! Please and Thank You.
You are doing this:-
cin >> testScores[count];
This would accept all integers positive as well as negative. One way is to check the number, if it's positive then insert it into array.

C++ Programming Errors: C4703, C4700

i am relatively new to programming, i just learned c++ and i am getting 2 errors for a HW assignment from school;
Error 2 error C4700: uninitialized local variable 'searchValueptr' used Line 83
and
Error 3 error C4703: potentially uninitialized local pointer variable 'createdArray' used
Line 70
I cannot for the life of me figure out why or how to fix it can some one help me please.
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
// prototypes
int *createArray(int &size);
void findStats(int *arr, int size, int &min, int &max, double &average);
int *searchElement(int *arr, int size, int *element);
int main ()
{
int choice, size;
bool menuOn = true;
while (menuOn == true)
{
cout << "1 - Create and initialize a dynamic array.\n";
cout << "2 - Display statistics on the array.\n";
cout << "3 - Search for an element.\n";
cout << "4 - Exit.\n";
cout << "Enter your choice and press return: ";
cin >> choice;
cout << endl;
switch (choice)
{
case 1:
int *createdArray;
cout << "Please enter a size for the array: ";
cin >> size;
createdArray = createArray(size);
for (int x=0; x < size; x++)
{
cout << "Value of array at index " << x << ": " << createdArray[x] << endl;
}
cout << endl;
break;
case 2:
int minNum;
int maxNum;
double avgNum;
findStats(createdArray, size, minNum, maxNum, avgNum);
cout << endl << "The maximum number is: " << maxNum;
cout << endl << "The minimum number is: " << minNum;
cout << endl << "The average number is: " << avgNum;
cout << endl << endl;
break;
case 3:
int *searchValueptr;
int searchValue;
cout << "Enter a value to search for: ";
cin >> searchValue;
*searchValueptr = searchValue;
searchElement(createdArray, size, searchValueptr);
break;
case 4:
cout << "Thanks for using this program";
menuOn = false;
break;
default:
cout << "Not a Valid Choice. \n";
cout << "Choose again.\n";
cin >> choice;
break;
}
}
cout << endl;
system("pause");
return 0;
} // end function main ()
int *createArray(int &size)
{
unsigned seed = time(0);
srand(seed);
int *randArray = new int [size];
for (int x=0; x < size; x++)
{
randArray[x] = rand();
}
cout << endl;
return randArray;
}
void findStats(int *arr, int size, int &min, int &max, double &average)
{
min = arr[0];
max = arr[0];
double sum = 0;
for (int count = 0; count < size; count++)
{
if (min >= arr[count])
{
min = arr[count];
}
if (max <= arr[count])
{
max = arr[count];
}
sum += arr[count];
}
average = sum/size;
}
int *searchElement(int *arr, int size, int *element)
{
bool match = false;
for (int count = 0; count < size; count++)
{
if (arr[count] == *element)
{
match = true;
}
}
if (match)
{
cout << "Match Found at: " << element;
return element;
}
else
{
cout << "No Found";
return 0;
}
}
either use
searchValueptr = &searchValue;
or send the pass the address of searchValue
searchElement(createdArray, size, &searchValue);
break;