How to throw out a value in an array - c++

I'm creating a diving calculator program. Asks for the level of difficulty. the program then asks for the 7 scores from judges 1-7, stores them in an array, throws out the smallest and largest values then finds the sum of the the remaining. The remaining is then multiplied by the level of difficulty and 0.6. My issue is with my findLargest function, it's as if i'm not calling it all. CODE:
#include <iostream>
using namespace std;
int judgesScore[7]; //array name
float difficulty; //between 1.2 & 3.8
float finalScore;
void collectInput() {
int input;
for (int i=0; i < 7; i++){
input = -1;
while (input < 0 || input > 10) {
cout << "Enter the score of judge " << i+1 << ": ";
cin >> input;
}
judgesScore[i]=input;
}
}//end collectInput
int findsmallest () {
int smallest = 0;
for (int i = 1; i < 7; i++){
if (judgesScore[i] < judgesScore[smallest]){
smallest = i;
}
}
return smallest;
}//end smallest
int findlargest () {
int largest = 0;
for (int i = 1; i < 7; i++){
if (judgesScore[i] < judgesScore[largest]){
largest = i;
cout << "the largest is: "<<largest;
}
}
return largest;
}//end largest
int sumOfScore(){
int smallest = findsmallest();
int largest = findlargest();
int sum = 0;
for(int i =0; i <7; i++){
if( i !=smallest && i !=largest){
sum+= judgesScore[i];
}
}
return sum;
}
int main(int argc, char *argv[]){
while (!(difficulty >= 1.2 && difficulty <= 3.8) ){
cout << "Please enter the level of difficulty from 1.2 - 3.8: ";
cin >> difficulty;
}//end of while
collectInput();
cout << "the sum of scores is "<<sumOfScore() << endl;
finalScore = (sumOfScore() * difficulty) * 0.6;
cout
<< "at a difficulty level of " << difficulty << "\n"
<< "Final Score: " << finalScore << "\n";

Try this! Your printing the index for the largest should be outside the for loop, and some modifications in comparison.
int findlargest () {
int j = 0, largest = judgesScore[0];
for (int i = 1; i < 7; i++){
if (judgesScore[i] < largest){
largest = judgesScore[i];
j = i;
}
}
cout << "the largest is: "<< j;
return j;
}//end largest

your findlargest function is the same with findsmallest function, so the result is smallest == largest.Please careful~ ^_^
int findlargest () {
int largest = 0;
for (int i = 1; i < 7; i++){
if (judgesScore[i] > judgesScore[largest]){
largest = i;
cout << "the largest is: "<<largest;
}
}
return largest;
}//end largest

Related

Smallest composite number in array

I am totally new to programming and I am bit stuck on my code. I wrote code where I wanted to find smallest composite number in array(using only low-level arrays). When I wrote down like size of array 3 and enter 1 2 77, than it throws out random 16. Can you explain why is this happening and perhaps give some solution how to fix this.
#include<iostream>
using namespace std;
int fun(int n)
{
int arr[n];
int mini = arr[0];
for (int i = 0; i < n; i++)
cin >> arr[i];
for (int i = 0; i < n; i++)
{
for (int j = 2; j < arr[i]; j++)
{
if (arr[i] % j == 0)
{
if (mini > arr[i])
{
mini = arr[i];
}
else
{
mini = mini;
}
break;
}
}
}
return mini;
}
int main()
{
int n;
cout << "Size of array: ";
cin >> n;
cout << "Write " << n << " numbers: " << fun(n) << endl;
return 0;
}

Sequence for numbers in a vector

void Numbers()
{
do
{
cout << "Enter the value for the sequence: ";
cin >> K;
if ( K <= 3)
{
cout << "Write a bigger number!" << endl;
}
} while(K <= 3);
cout << "Enter the first number: ";
cin >> N;
}
double Sum()
{
vector<double> arr(K);
arr.push_back(N);
for (int i=0; i < arr.size(); i++)
arr.at(i)=i;
cout << "Vector contains: ";
for (int i=0; i < arr.size(); i++)
cout << arr.at(i);
int main()
{
Numbers();
Sum();
return 0;
}
Write a program that generates sequence of K (K > 3) numbers as follows:
The members of the above sequence are obtained as follows:
the first element is N;
the second one is N + 1;
the third - N * 2.
In other words, we consistently add 1 to each element and put it to the end of the sequence, then multiply it by 2 and again, put the product to the end of the sequence. Choose and implement a suitable data structure that can be used to generate the above sequence of numbers.
The users should enter values for K and first element N.
This is my current code(in the code above). I don`t realy know where to go from here onward to be completely honest. Any suggestions on how to create the sequence from the condition above?
You can use this code to get what you want:
#include <iostream>
#include <vector>
using namespace std;
vector<double> createOutputArray (int K, int N)
{
vector<double> arr;
int tmp = N;
arr.push_back(tmp);
for(int i=1; i+2<=K; i+=2)
{
arr.push_back(++tmp);
arr.push_back(tmp * 2);
tmp *= 2;
}
if(K % 2 == 0)
arr.push_back(++tmp);
return arr;
}
int main()
{
int K;
double N;
do
{
cout << "Enter the value for the sequence: ";
cin >> K;
if ( K <= 3)
{
cout << "Write a bigger number!" << endl;
}
} while(K <= 3);
cout << "Enter the first number: ";
cin >> N;
vector<double> output = createOutputArray(K, N);
for (int i=0; i < output.size(); i++)
{
cout << output.at(i);
if(i < output.size()-1)
cout << ",";
else
cout << endl;
}
return 0;
}
Here is one possibility, using a generator to produce the next element in the sequence.
class Seq
{
public:
Seq(int n) : n(n) {}
int operator*() const { return n; }
Seq operator++(int)
{
Seq old(n);
n = fns[fn](n);
fn = 1 - fn;
return old;
}
private:
int n;
int fn = 0;
std::function<int(int)> fns[2] = {[](int x) { return x + 1; },
[](int x) { return x * 2; }};
};
int main()
{
int N = 1;
int K = 20;
Seq seq(N);
for (int i = 0; i < K; i++)
{
std::cout << *seq++ << ' ';
}
std::cout << std::endl;
}

Consecutive comparisons using if statements inside a while loop

I am supposed to compare two consecutive integers, i and j, that are given from a list of integers separated by whitespace which end with a 0 and, if i is less than j, I compare j to max and i to min. If the opposite, I compare j to min and i to max. The output is supposed to be each comparison I do with max, min, i, and j. Additionally, the list must be greater than 2 integers. If it is less then I am supposed to output 0. However the program does not seem to execute the if statements correctly.
int i = 1;
int j;
int max = 0;
int min = 0;
int counter = 0;
while (i != 0) {
cin >> i;
if (counter == 0) {
cout << 0 << endl;
i = min;
j = max;
} else if (counter == 1) {
cout << 0 << endl;
i = min;
j = max;
} else {
if (i < j) {
if (j > max) {
cout << j << " " << max << endl;
max = j;
}
if (i < min) {
cout << i << " " << min << endl;
min = i;
}
}
else {
if (j < min) {
cout << j << " " << min << endl;
min = j;
}
if (i > max) {
cout << i << " " << max << endl;
max = i;
}
}
}
j = i;
counter += 1;
}
}
You are overwriting i (and j) in the first iteration of the while loop (counter == 0) with min which is 0. As your while says while (i != 0) you immediately exit, after one while iteration. The ifs should be fine.
Input and counter logic can be improved by:
#include <iostream>
using namespace std;
int main(){
int i;
int j;
int max = 0;
int min = 0;
int counter = 0;
bool flag = true; // fix the input logic
while (flag){
cin >>i;
cin>>j;
if(i==0 || j==0){
flag = false;
break;
}else{
counter ++;
if(counter < 2 ){
//your code
}
else if(i<j){
cout<<"i: "<<i<<" j: "<<j;
j = max;
i = min;
// your code
}
else if(i>j){
//your code
}
}
}
return 0;
}

(c++) Function calculating average returns (first score / total number of scores)?

So the program gathers a number of scores specified, then displays them in ascending order, then is supposed to show the average score. But right now, it only takes the first score displayed, and is divided by the number of scores. How can I make it display correctly?
#include <iostream>
#include <iomanip>
using namespace std;
void sortArray(int*, int);
double getAverage(int*, int);
int main()
{
int *scores;
int num_Tests;
cout << "How many test scores would you like to enter?" << endl;
cin >> num_Tests;
scores = new int[num_Tests];
cout << "\nEnter score number (do not use negative numbers):\n";
for (int count = 0; count < num_Tests; count++)
{
cout << count + 1 << ". ";
cin >> scores[count];
}
sortArray(scores, num_Tests);
cout << "\n\n";
cout << "\n\n________________________________________________________________________________" << endl;
cout << "Test Score List (in ascending order)" << endl;
cout << "________________________________________________________________________________" << endl;
for (int count = 0; count < num_Tests; count++)
{
cout << count + 1 << ". ";
cout << scores[count] << endl;
}
cout << "\nAverage test score: " << getAverage(scores, num_Tests) << endl;
return 0;
}
double getAverage(int *scores, int size)
{
double ttlScore = 0.0;
double avgScore = 0.0;
ttlScore += *scores;
avgScore = ttlScore / size;
return avgScore;
}
void sortArray(int *scores, int size)
{
int temp;
bool swap;
do
{ swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (scores[count] < scores[count + 1])
{
temp = scores[count];
scores[count] = scores[count + 1];
scores[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
You need to loop through your scores array to add them, you cannot just do ttlScore += *scores, it needs to be ttlScore += scores[index]
Example:
double getAverage(int *scores, int size)
{
double ttlScore = 0.0;
for (int i = 0; i < size; i++)
{
ttlScore += scores[i];
}
return ttlScore / size;
}
getAverage logic is not correct. You are adding just first number. Add is as below:
double getAverage(int *scores, int size)
{
double ttlScore = 0.0;
double avgScore = 0.0;
for(int i=0;i<size;i++)
{
ttlScore += *(scores+i);
}
avgScore = ttlScore / size;
return avgScore;
}
*scores references the start of the array. You need to sum over all of the values of the array pointed to:
for (int i=0; i < size; i++)
{
ttlScore += scores[i];
}
avgScore = ttlScore / size;
return avgScore;
I think these will help you,
double getAverage(int *scores, int size){
int i, sum = 0;
double avg;
for (i = 0; i < size; ++i)
{
sum += scores[i];
}
avg = (double)sum / size;
return avg;
}
Try and let me know if any problem?
I'm new to programming but here is what I have.
//average function
double average(int* pnData)
{
double result;
int sum = 0; //declare and initialize our variables
int count = 0;
for (int i = 0; i < pnData[i]; i++)
{
sum += pnData[i]; //sum = sum + elements of our array
count++; //increment count
}//end of for loop
result = sum/(double)count;
return result;
}//end of function average
This way you do not have to know the length or count of the array.

Average of odd cells?

For an exercise I am doing, I am supposed to find out the average of the items contained in odd numbered cells of an array and some other things. Finding the average of the odd numbered cells in the only thing I'm having a problem with. Here is my code, what am I doing wrong? The final function is the odd numbered cells average function. Thanks.
#include <iostream>
#include <iomanip>
using namespace std;
const int SIZE = 1000;
int randoms[SIZE];
int sum2 = 0;
int top = 0;
int maximum = 0;
int highest(int randoms[]);
int findsum(int randoms[]);
int average(int randoms[], int sum);
int oddavg(int randoms[]);
int main()
{
int sum = 0;
int top = 0;
int avg = 0;
int oddaverage = 0;
for (int i = 0; i < SIZE; i++)
{
randoms[i] = (rand() % 5000 + 1);
cout << randoms[i] << setw(10) << " ";
}
cout << endl << endl;
cout << "The sum of the values in the array is ";
sum = findsum(randoms);
cout << sum << endl;
cout << endl << endl;
cout << "The highest value in the array is ";
top = highest(randoms);
cout << top << endl;
cout << endl << endl;
cout << "The average of all of the numbers in the array is ";
avg = average(randoms, sum);
cout << avg << endl;
cout << endl << endl;
cout << "The average of all of the numbers in the odd cells is ";
oddaverage = oddavg(randoms);
}
int findsum(int randoms[])
{
for (int i = 0; i < SIZE; i++)
{
sum2 += randoms[i];
}
return sum2;
}
int highest(int randoms[])
{
for (int i = 0; i < SIZE; i++)
{
if (randoms[i] > maximum)
{
maximum = randoms[i];
}
}
top = maximum;
return top;
}
int average(int randoms[], int sum)
{
int avg = 0;
for (int i = 0; i < SIZE; i++)
{
avg = (sum / SIZE);
}
return avg;
}
int oddavg(int randoms[])
{
int avg = 0;
int sum = 0;
for (int i = 0; i < SIZE; i++)
{
if (randoms[i] / 2 == 1)
{
sum += randoms[i];
}
}
avg = sum / SIZE;
return avg;
}
Doing the odd/even test (using modulo as many have suggested) in this case is totally redundant, since the loop doesn't do anything else.
You can just use a stride of 2 and start at the first odd element:
for (int i = 1; i < SIZE; i += 2)
{
sum += randoms[i];
}
Then it's just a matter of dividing out by half of SIZE. If that number is even, then there are SIZE/2 odd numbers. If it's odd, then there are lbound(SIZE/2)+1 odd numbers. Fortunately, you can take advantage of integer truncation and just do:
double avg = double(sum) / double((SIZE+1) / 2);
And you don't even have to worry about divide-by-zero =)
should be if (randoms[i] % 2 == 1)
Also you need to count the number of odd numbers.
randoms[i] / 2 == 1, this will be true only when a cell value is 2 or 3, this is certainly not what you need to do.
If you need to sum values of cells with odds index then it should be if (i % 2 == 1). If instead you are looking for odd values (in any cell) it should be if (randoms[i] % 2 == 1).
Mind that % is the modulo operation which returns the integer remainder of the integer division.
And since you are calculating an average you should divide by the found amount of elements, not by the total.
int oddavg(int randoms[])
{
int cnt = 0;
int avg = 0;
for (int i = 0; i < SIZE; i++)
{
if ( randoms[i] % 2 != 0 )
{
++cnt;
avg += randoms[i];
}
}
return ( cnt == 0 ? avg : avg / cnt );
}