Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
please help with c ++?
Modify this program to alow the user to enter name-score pairs. For each student taking a test, the user types a string representing the name of the student, followed by an integer representing the student's score. Modify both the sorting and average-calculating functions so they take arrays of structures, with each structure containing the name and score of a single student. In transversing the arrays, use pointers rather than array indices. So yeah if someone could help me do all this with the program below
#include <iostream>
#include <iomanip>
using namespace std;
int sum(int);
int avg(int);
void sortArray(int [], int);
void showArray(int [], int);
int count,numdays;
int *sales;
int total,a,y;
int main()
{
cout<< " How many test scores are you going to enter? ";
cin>>numdays;
if(numdays<0)
{cout<<"only enter positive numbers"<<endl;
return 0;}
sales = new int[numdays];
cout<<"Enter the scores below.\n";
for(count = 0; count< numdays; count++)
{
cout<<"Score "<<(count + 1) << ": ";
cin>> sales[count];
if(sales[count] <0)
{cout<<"only enter positive numbers"<<endl;
return 0;} }
cout<<" The average is "<<avg(y)<<endl;;
sortArray(sales, numdays);
cout<< "The Sorted scores are:\n";
showArray(sales, numdays);
return 0;
}
int sum( int x)
{
for ( count = 0 ; count< numdays; count ++)
{total += sales[count];}
return total;
}
int avg ( int r)
{
int g = sum(a)/numdays;
return g;
}
void sortArray(int array[], int size)
{
bool swap;
int temp;
do
{swap = false;
for(int count = 0; count<(size-1);count ++)
{
if(sales[count]>array[count + 1])
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
void showArray(int array[], int size)
{
for (int count = 0; count< size;count++)
cout<<array[count]<<" ";
cout<<endl;
}
The specs for the program above were these :
Write a program that dynamically allocates an array large enough to hold a user-defined number of test scores. Once all the scores are entered, the array should be passed to a function that sorts then in ascending order. Another function shoud be called that calculates the average score. The program should display the sorted list of scores and averages with appropiate headings. Use pointer notation rather than array notation whenever possible. Input validation : do not accept negative numbers for test scores.
4 Answers
- Wertle WooLv 610 years agoFavorite Answer
Both of the above answers are rather useless. One tells you to read a book, the other advises with C-style preprocessor constants and structs. Give me a moment to read through your longass post and I'll give you a real answer.
EDIT:
1. Don't get into the bad habit of using global variables. Pass the local variables into the function by value to protect them from being changed by the function, or pass them by reference if you want the function to change them.
2. It's good practice to initialize your variables to a default value so you don't run the risk of trying to access a variable of undefined value.
3. You don't really have any input validation. What you have is an if statement that checks for one unwanted condition and displays a message if the input matches this undesired value. If statements should be used to test for a DESIRED value, because there can be an infinite amount of unwanted values. Undesired values should be dealt with by the else statement.
4. After accepting the input, if it's invalid you do nothing with it and let the program end (return 0) from your if statement. You should only have ONE statement in main that returns 0. Anything else should be within functions or loops that exit under appropriate circumstances (they will eventually hit the return statement anyway), but nothing should end the program except the end of main.
5. Your for loop is again not using input validation, but exiting the program if one particular undesired condition is met.
6. Because I told you not to use global variables, this means you'll have to change your functions that access global variables. Also, it doesn't really matter which naming convention you follow, but you should at least follow one pattern for naming your variables, functions, preprocessor constants, etc., to make them easier to identify. For simplicity, I'm going to use lowercase variable names and TitleCase function names.
7. You mismatched your array name in your sortArray function. In one place you refer to it as sales, in another place you refer to it as array (the local parameter name). You probably did this as a direct result of your bad global variable habits.
Below is a link to a text file with a modified version of your code, with the above-mentioned corrections.
- MencellatorLv 410 years ago
Ugh, this is way too long a question to answer...
Read the book "Accelerated C++", it has pretty much EXACTLY the same problem (maybe that's whey they got it from?) and teaches you how to build the program step by step across the first few chapters of the book.
- 10 years ago
you can make a struct:
#define maxN 10000 //(or whatever the maximum number of students is)
struct data
{
string name;
int score;
} arr[maxN];
for the sorting function, you can use the simple bubble sort
//the following sort function will sort according to the students' scores in descending order
bubbleSort(student data[])
{
int i, j;
for(i=0;i<nStudents;i++)// nstudents is a variable = to the number of students
for(j=i+1;j<nStudents;j++)
if(data[i] < data[j]) swap(data[i], data[j]);
}
for scanning, you can do that:
int nStudents = 0, i;
for(i=0;i<nStudents;i++)
cin >> arr[nStudents].name >> arr[nStudents++].score;
- ?Lv 44 years ago
This code will print 2 random integers on the stdout. printf("%d %d",i,j) will print a million 2 and printf("%d %d",j,i) will print 2 a million(because of the fact this is the output you asked!).