Primary expression before Double which is in my Array? - c++

IT keep asking me for a primary expression before Double which is in my int Array and i dont know what to do. i have midterms tomorrow ugh this hurts my brain. All other input would be helpful thanks :)
#include <iostream>
#include <iomanip>
using namespace std;
void get_input(double array[50], int& sizearray1)
{
cout << "How many doubles do you wish to add?" << endl;
cin >> sizearray1;
while(sizearray1 < 1 || sizearray1 > 50) {
cout << "Error: That is an invalid number! You must enter a value between 1 and 50.\nHow many doubles do you wish to add?" << endl;
cin >> sizearray1;
}
for(int n=1;n<sizearray1;n++){
cout << "Enter Double " << n << endl;
cin >> array[n];
}
}
double calcSum(double array[50],int sizearray1)
{
int sum
cout << "The sum is ";
for(n=1,n<sizearray1,n++){
sum += array[n]
}
cout << sum << endl;
}
void printArray(double c[],int sizearray1)
{
cout << "The array contains:\n";
for(int n=1;n<sizearray1;n++){
cout << setprecision(2) << showpoint << fixed << c[n] << " ";
}
calcSum(double array[50],int sizearray1);
}
int main()
{
double array1[50];
int sizearray1 = 0;
get_input(double array[50],int sizearray1);
printArray(double array[50],int sizearray1);

Firstly I'm going to start off with some errors, as THIS code can't compile. No way.
You're missing a semicolon at int sum line 21. Your for loop at line 23 has commas instead of semicolons. You're using a undefined variable n = 1 in the same loop, you have to declare it before doing so for(n=1,n<sizearray1,n++). Again in the same for loop at line 24 you forgot another semicolon sum += array[n].
Back to your question:
You can't give a function a type parameter, you would actually try to declare a variable there. Also please don't declare a function like this double calcSum (double array[50], int sizearray1) as this is really not what you want... use this instead double calcSum (double array[/*Nothing here*/], int sizearray1).
Please check your whole code and fix all errors. This is a fix of me. Of course it doesn't work, your code is a bit to messy, check all the functions and give them the right parameters.
#include <iostream>
#include <iomanip>
using namespace std;
void get_input (double array[], int& sizearray1)
{
cout << "How many doubles do you wish to add?" << endl;
cin >> sizearray1;
while (sizearray1 < 1 || sizearray1 > 50)
{
cout << "Error: That is an invalid number! You must enter a value between 1 and 50.\nHow many doubles do you wish to add?" << endl;
cin >> sizearray1;
}
for (int n = 1; n < sizearray1; n++)
{
cout << "Enter Double " << n << endl;
cin >> array[n];
}
}
double calcSum (double array[], int sizearray1)
{
int sum;
cout << "The sum is ";
for (int n = 1; n < sizearray1; n++)
{
sum += array[n];
}
cout << sum << endl;
}
void printArray (double c[], int sizearray1)
{
cout << "The array contains:\n";
for (int n = 1; n < sizearray1; n++)
{
cout << setprecision(2) << showpoint << fixed << c[n] << " ";
}
calcSum (array[50], sizearray1); //This still can't work, the variable "array" has not been declared
}
int main()
{
double array1[50];
int sizearray1 = 0;
get_input (array[50], sizearray1); //Again "array" has not been declared
printArray (array[50], sizearray1); //Again "array" has not been declared
return EXIT_SUCCESS;
}

When calling a function don`t include the type like :
calcSum(double array[50],int sizearray1);
But simply use the variable names :
calcSum(array[50],sizearray1);
same with your get_input() and printArray() functions.
And in your printArray() function, I think you are recieving the passed array into the variable c. So it should be :
calcSum(c[50],sizearray1);
And I think you are trying to pass the whole array, then you just have to use the arrayname:
calcSum(c,sizearray1);
c[50] will only pass the element at 50th position in the array.

Related

I got infinite loop while practicing array in C++ to find reversed number

Hye, Im a beginner trying to learn C++ language. This is my code that I tried to find reverse input numbers using array. Can help me point my mistakes since I always got infinite loop.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int ARRAY_SIZE=50;
int size[ARRAY_SIZE];
unsigned short int i;
cout << "You may enter up to 50 integers:\n";
cout << "\nHow many would you like to enter? ";
cin >> size[ARRAY_SIZE];
cout << "Enter your number: \n";
for (int i = 0; i < ARRAY_SIZE; i++)
{
cin >> size[i];
}
cout << "\nYour numbers reversed are:\n";
for (i = size[ARRAY_SIZE] - 1; i >= 0; i++)
cout << " size[i]" << " ";
}
Your infinite loop is because i is unsigned, so i >= 0 is always true.
Here's a C++-ified version:
#include <iostream>
#include <vector>
int main() {
std::cout << "You may enter up to 50 integers:\n";
std::cout << "\nHow many would you like to enter? ";
int count;
std::cin >> count;
// Use a std::vector which can be extended easily
std::vector<int> numbers;
for (int i = 0; i < count; ++i) {
std::cout << "Enter your number: \n";
int v;
std::cin >> v;
// Add this number to the list
numbers.push_back(v);
}
std::cout << "\nYour numbers reversed are:\n";
// Use a reverse iterator to iterate through the list backwards
for (auto i = numbers.rbegin(); i != numbers.rend(); ++i) {
// An iterator needs to be de-referenced with * to yield the value
std::cout << *i << " ";
}
std::cout << std::endl;
return 0;
}
There's many problems in your original code, but the clincher is this:
for (i = size[ARRAY_SIZE] - 1; i >= 0; i++)
cout << " size[i]" << " ";
}
Since you keep adding to i through each cycle you'll never go below zero, especially not for an unsigned short int. This should be:
for (int i = count - 1; i > 0; --i) {
std::cout << numbers[i];
}
Presuming you have a thing called numbers instead of the bizarrely named size and the array size is count, not i, as i is generally reserved for iterators and loop indexes.

Integers not getting added up correctly but doubles are working fine

I know this isnt the right kind of question to be asking, but for the life of me I could not figure out what is causing this problem.
I need to write a problem that takes a set number of integers or doubles and returns their sum.
I have written the code to make this work, making sure to check each time I changed something.
#include<iostream>
using namespace std;
template <class T>
class totalClass
{
private:
T *p;
T Total;
T sum;
int size;
public:
T total(int x)
{
size = x;
p = new T[x];
for (int i = 0; i < size; i++)
p[i] = T();
if (size > 1)
{
for (int i = 0; i < size; ++i)
{
cin >> sum;
Total += sum;
}
}
return Total;
}
};
int main()
{
int size, result1;
double result2;
cout << "Enter: ";
cin >> size;
cout << "the number of ints you wish to enter: Enter: " << size << " integers:";
totalClass<int> test;
result1 = test.total(size);
cout << " Total = " << result1 << endl;
cout << "Enter: ";
cin >> size;
cout << "the number of doubles you wish to enter: Enter: " << size << " doubles:";
totalClass<double> test2;
result2 = test2.total(size);
cout << " Total = " << result2 << endl;
}
My doubles are getting added up correctly but my integer addition always seems to add up to some crazy number. Is there something wrong with my problem that I cannot see?
If you forget to initialize a variable and attempt to use it or do math with it, you might end up with "crazy numbers." Make sure all of your variables are Initialized.

Using arrays and strings together

So I'm trying to create an array that contains some user inputted names, and then associate those names with letter grades from tests (ex: A, B, C, D, F). My question is, how would I use an array to accept the user inputted names?
EDIT:
Sorry this is a bit long, I don't know what part to put that would help out. Totally new to C++ and I can't seem to find anything online regarding the matter, lol.
Here is some code. This program currently asks the user for test scores, then displays and drops the lowest test score, and finally, calculates the average of the scores without the lowest one. The end goal is to ask the user for 5 students names, and 4 scores for each student, then dropping the lowest score for each student and calculating the averages of ALL scores inputted regardless of student.
#include <iostream>
#include <string>
using namespace std;
void getScore(int &);
int findLowest(int [], int);
void calcAverage(int [], int);
int main () {
const int NUM_SCORES = 5;
int scores[NUM_SCORES];
cout << "Welcome to test averages." << endl;
cout << "Please enter scores for " << NUM_SCORES << " students." << endl;
cout << endl;
for (int i = 0; i < NUM_SCORES; i++) {
getScore(scores[i]);
}
for (int i = 0; i < NUM_SCORES; i++) {
cout << "Score " << (i + 1) << ": " << scores[i] << endl;
}
cout << endl;
cout << "The lowest of these scores is " << findLowest(scores, NUM_SCORES) << endl;
calcAverage(scores, NUM_SCORES);
return 0;
}
void getScore(int & s) {
s = -1;
cout << "Please enter a test score: ";
cin >> s;
while (s < 0 || s > 100) {
cout << "Score range must be from 0-100" << endl;
cout << "Please re-enter a score: ";
cin >> s;
}
}
int findLowest(int theArray [], int theArraySize) {
int lowest = theArray[0];
for (int i = 1; i < theArraySize; i++) {
if (theArray[i] < lowest) {
lowest = theArray[i];
}
}
return lowest;
}
void calcAverage(int theArray [], int theArraySize) {
int sum = 0;
for (int i = 0; i < theArraySize; i++) {
sum += theArray[i];
}
double average = (sum - findLowest(theArray, theArraySize)) / (theArraySize - 1.0);
cout << "The average is " << average << endl;
}
Try getline from #include <string>
std::string names[5];
for (int i = 0; i < 5; ++i){
getline(std::cin, names[i]);
}

What is a way to solve this with loops in c++?

I've been coding all night so my head is in a state of shock. I'm trying to do the following, but I just don't get what our professor wants us to do. "Input an unsigned and call it number.. Then input number double values, then output the sum and product of the numbers. (If number is zero, then 0 doubles will be input; the sum of 0 numbers is 0, and the product of 0 numbers is 1)"
I could really appreciate if someone could help me with this. Thank you.
Edit:
This is what i have so far, The thing I'm currently confused with is on how to make the unsigned variable be the number of double inputs inside of the loop.
unsigned number, x;
double double_num, sum;
cout << "Input the number of value: \n";
cin >> number;
for (x = 0; x > number; x++) {
cin >> double_num;
}
return 0;
What're you having difficulty with?
Here's a start:
// Input an unsigned and
unsigned int g;
cout << "Please Enter an unsigned int value, g" << endl;
cin >> g;
In your loop, you have:
for (x = 0; x > number; x++) {
Which means:
x is set to Zero.
While x is bigger than some-number, keep going...
When do you think zero is bigger than a number such as 5??
How many times do you think that loop will run??
int main(void)
{
unsigned g;
double product(1);
double sum(0);
cout << "Input the number of value: \n";
cin >> g;
for (unsigned x = 0; x < g; x++) {
double n;
cout << "Input a number: " <<endl;
cin >> num;
product *= num;
sum += num;
}
cout << "The product is" << product << "\n";
cout << "The sum is" << sum << "\n" << endl;;
return 0;
}
You need something like the following:
#include<iostream>
#include<cstdlib>
int main()
{
unsigned int n;
std::cin >> n;
double g, p=1, s=0;
while (n-->0 && std::cin >> g) p*=g,s+=g;
return std::cin
? std::cout << s << std::endl << p << std::endl, EXIT_SUCCESS
: (std::cerr << "Failed to read all inputs" << std::endl, EXIT_FAILURE);
}
You should be sure that you understand each statement and be prepared to justify your choices; if not, you probably haven't learnt anything and will be unlikely to pass your course.

Vectors to find Median - homework

I'm a relatively new learner to C++ and I've been having some trouble. If you guys read the title, this is a homework problem (just letting you guys know out there) and I'm not really sure as to where my error is. Using GIT Bash, I can't see why this isn't compiling (or maybe i just don't know how to read it). I feel like i've touched upon all the bases and would appreciate a quick look over to see if my mistake is blaringly obvious. I've done a couple looks through stackoverflow and so the inputting values into a vector was used from another post but i've modified it a bit. In addition, I added in a sort for the vector from smallest to largest.
Also, how can I change the for statement to allow for variable #'s in the vector?
#include <iostream>
#include <vector>
using namespace std;
double showMedian(const vector<int> & vecmedian, int size)
{
int middle;
double average, median;
middle = size / 2.0;
if (size % 2 == 0)
{
median = (vecmedian[middle] + vecmedian[middle + 1]) / 2.0;
cout << "The median is: " << average << endl;
}
else
{
median = vecmedian[middle + 0] / 1.0;
cout << "The median is: " << median << endl;
}
return median;
}
int main()
{
int n,input, i;
vector<int> vecmedian;
vector<int>::iterator itr;
cout << "Enter the amount of numbers: ";
cin >> n;
cout << "Enter your numbers to be evaluated: " << endl;
while (vecmedian.size() < n && cin >> input){
vecmedian.push_back(input);
}
for(i = 1; i < 10; ++i){
for(itr = vecmedian.begin(); itr != vecmedian.end(); ++itr){
if(vecmedian[i] < *itr){
vecmedian.insert(itr, vecmedian[i]);
break;
}
}
if(itr == vecmedian.end())
vecmedian.push_back(vecmedian[i]);
}
showMedian();
return 0;
}
Point 1
When making function prototypes, you need to keep them consistent with the actual definition of the function.
You have:
void showMedian();
As a function-prototype but you have:
double showMedian(int *vecmedian, int size)
As the actual definition. They both need to be consistent.
Since you have not declared an array, maybe change the parameters of showMedian to:
double showMedian(const vector<int> & vecmedian, int size)
Point 2
if(nums[i] < *itr)
Where is nums declared?
Point 3
If you want to use the definition of showMedian, then use the parameters that it uses assuming you made the changes above (and assuming n is size).
showMedian(vecmedian, n);
Edit
With all the consulting in the comment section and the new updated OP Question, here is a fairly solid program which finds the median in a vector:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
double showMedian(const vector<double> & vecmedian, int num);
int main()
{
unsigned int n;
double input;
vector<double> vecmedian;
// cout << "Enter the amount of numbers: ";
do {
cout << "Enter the amount of numbers: ";
while(!(cin >> n)){
cout << "Wrong input" << endl;
cout << "Enter the amount of numbers: ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
if (n == 0)
{
cout << "Invalid, size must be greater than 0" << endl;
}
} while (n == 0);
// cout << "Enter your numbers to be evaluated: " << endl;
for (int i = 1; i <= n; ++i)
{
cout << "Enter number here (" << ((n + 1) - i) << " number/s remaining): ";
while(!(cin >> input)){
cout << "Wrong input" << endl;
cout << "Enter number here (" << ((n + 1) - i) << " number/s remaining): ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
vecmedian.push_back(input);
}
// while (vecmedian.size() < n && cin >> input){
// vecmedian.push_back(input);
// }
sort(vecmedian.begin(), vecmedian.end());
showMedian(vecmedian, vecmedian.size());
return 0;
}
double showMedian(const vector<double> & vecmedian, int num)
{
int middle;
double median;
middle = (num / 2);
if (num % 2)
median = vecmedian[middle];
else
median = (vecmedian[middle - 1] + vecmedian[middle]) / 2.0;
cout << "The median is: " << median << endl;
return median;
}