HELP please. I'm new to this so please be nice and descriptive. I coded this in Visual studio and the goal is to find out how many of each L, M, and S trays I need based on the number of people attending. I am trying to divide by remainder and I'm getting an error on the last two lines. "expression must have integral or unscoped enum type" --- I don't even know what that means. English, please?
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
//prompt user
cout << "Please enter number of guests attending event:";
double attendees;
cin >> attendees;
double large_trays = attendees / 7;
double medium_trays = large_trays % 3;
double small_trays = medium_trays % 1;
The problem is that you are using a 'double' type variable. However the modulo operator is only available for integer type variables like 'int'.
Either use 'int' instead of 'double' or use type casting.
double medium_trays = (int)large_trays % 3;
This exception is being thrown because you are trying to use the modulus arithmetic operator '%' on a non-integer. See this question: Why does modulus division (%) only work with integers?
Try this:
double large_trays = attendees / 7;
int large_trays_rounded = (int)large_trays;
int medium_trays = large_trays_rounded % 3;
int small_trays = medium_trays % 1;
Related
I want to make an array, and inside this array there are pointers, like this:
int *arrp[size]; and I want the user to enter the size of it.
I tried to do this:
#include <iostream>
using namespace std;
int main ()
{
int size;
cout << "Enter the size of the array of pointers" << endl;
cin >> size;
int *arrp[size];
return 0;
}
but this doesn't work.
I also tried to do this:
#include <iostream>
using namespace std;
int main ()
{
int size;
cout << "Enter the size of the array of pointers" << endl;
cin >> size;
int* arrp[] = new int[size];
return 0;
}
also doesn't work, can someone help?
The error of the first code is that the size must be constant, I tried to fix that by writing the 2nd code but it gives an error for the word "new" in line 9:
E0520 initialization with '{...}' expected for aggregate object
and another error for the size in the same line:
C2440 'initializing': cannot convert from 'int *' to 'int *[]'
To make an array of pointers you should type: int** arr = new int*[size]
we type 2 stars '*', the first mean a pointer to an integer, the second means a pointer to the pointer to the integer, and then we make a place in the memory for those pointers by typing = new int*[size], you can use this as a 2D array that stored in the heap (not the stack) go to this website to know the difference: https://www.geeksforgeeks.org/stack-vs-heap-memory-allocation/.
to know more about how to use an array of pointers to a pointer to an integers you can see this video: https://www.youtube.com/watch?v=gNgUMA_Ur0U&ab_channel=TheCherno.
I am fairly new to C++. I was practicing some ds,algo.This code looks fine to me, but I am getting some error about function not taking 2 arguments. Though I get some error asked in stackoverflow none of the cases match my problem.
#include <iostream>
#include <algorithm>
int ropecutting(int n, int *cuts){
if (n == 0)
return 0;
if (n < 0)
return -1;
int res = std::max(ropecutting(n-cuts[0], cuts), ropecutting(n-cuts[1], cuts), ropecutting(n-cuts[2], cuts));
if(res == -1) return -1;
return res+1;
}
int main(){
int n, cuts[3];
std::cin >> n;
for(int i = 0; i < 3; i ++)
std::cin >> cuts[i];
std::cout << ropecutting(n, cuts);
}
The error I get is,
main.cpp
G:\software_installation\Visual Studio Community 2017\VC\Tools\MSVC\14.16.27023\include\xlocale(319): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
G:\software_installation\Visual Studio Community 2017\VC\Tools\MSVC\14.16.27023\include\algorithm(5368): error C2064: term does not evaluate to a function taking 2 arguments
G:\software_installation\Visual Studio Community 2017\VC\Tools\MSVC\14.16.27023\include\algorithm(5367): note: see reference to function template instantiation 'const _Ty &std::max<int,int>(const _Ty &,const _Ty &,_Pr) noexcept(<expr>)' being compiled
with
[
_Ty=int,
_Pr=int
]
G:\software_installation\Visual Studio Community 2017\VC\Tools\MSVC\14.16.27023\include\algorithm(5368): error C2056: illegal expression
Wishing someone would point me out in the right direction. Thank you.
Of the overloads of std::max, the only one which can be called with three arguments is
template < class T, class Compare >
constexpr const T& max( const T& a, const T& b, Compare comp );
So since it receives three int values, that function is attempting to use the third value as a functor to compare the other two, which of course doesn't work.
Probably the simplest way to get the maximum of three numbers is using the overload taking a std::initializer_list<T>. And a std::initializer_list can be automatically created from a braced list:
int res = std::max({ropecutting(n-cuts[0], cuts),
ropecutting(n-cuts[1], cuts),
ropecutting(n-cuts[2], cuts)});
I am trying to use the function dspr in Openblas with Rcpp.
the purpose of dspr is:
A := alpha*x*x**T + A
In my case, firstly I define A as a matrix with all the elements are 0, alpha=1, x=(1,3), so, the final matrix A should be {(1,3),(3,9)}, but I never get the right result, I set the parameters as follow:
cblas_dspr(CblasColMajor,CblasUpper,2, 1, &(x[0]),1, &(A[0]));
Can anyone tell me how to set the right parameters of dspr? Thanks.
The file /usr/include/cblas,h on my machine shows the following signature for the C interface of the BLAS:
void cblas_dspr(const enum CBLAS_ORDER order, const enum CBLAS_UPLO Uplo,
const int N, const double alpha, const double *X,
const int incX, double *Ap);
Try that. You get the beginning of Rcpp vectors via x.begin() or via &(x[0]) as you did.
There is nothing pertinent to Rcpp here though.
Repeated from your own post: The BLAS dyadic product performs
A := alpha*x*x' + A
So A would need to be initialized with zero values.
In addition do not forget that A is an upper triangular matrix.
For further reading I recommend these links:
https://developer.apple.com/library/ios/documentation/Accelerate/Reference/BLAS_Ref/Reference/reference.html
https://github.com/foundintranslation/Kaldi/blob/master/tools/ATLAS/interfaces/blas/C/src/cblas_dspr.c
However, you wanted an example. Here goes:
/** dspr_demo.cpp */
#include <cblas.h>
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv)
{
int n=2;
double* x = (double*)malloc(n*sizeof(double));
double* upperTriangleResult = (double*)malloc(n*(n+1)*sizeof(double)/2);
for (int j=0;j<n*(n+1)/2;j++) upperTriangleResult[j] = 0;
x[0] = 1; x[1] = 3;
cblas_dspr(CblasRowMajor,CblasUpper,n,1,x,1,upperTriangleResult);
double*& A = upperTriangleResult;
cout << A[0] << "\t" << A[1] << endl << "*\t" << A[2] << endl;
free(upperTriangleResult); free(x);
return EXIT_SUCCESS;
}
I get an error message saying expression must have (pointer-to-) function type. what am i doing wrong? i just started coding, i know i suck lol. I don't understand how to get the formula for the distance to work.
#include <cmath> //headerfile
#include <iomanip>
#include <iostream>
enter code here
using namespace std;
int main()
{
double d;
double t;
double g;
char choice ='y';
//output numbers to console
while (choice == 'y' || choice =='Y')
{
cout<<"Please input a value for the time"<< endl<<endl;
cin>>t;
g = 32;
d = (g)(t*t);
if (t<0)
cout<<"You cannot have a negative time"<<endl<<endl;
else
cout<<setw(8)<<fixed<<setprecision(2)<<"\n""The distance the ball has fallen is "<<d<<" feet"<<endl<<endl;
cout<<"Would you like to run this again? y for yes, any other key for no."<< endl<<endl;
cin>>choice;
cout<<endl;
}
system ("Pause");
return 0;
}
If (g)(t*t) is supposed to be a normal multiplication operation, then it should be g*t*t.
In your code, g is a double, but you are using it as if it was a pointer to a function (d = (g)(t*t);). If what you really want is to multiply t*t by g, you forgot an *:
d = (g)*(t*t);
I am getting all kinds of errors when passing my array to this function. The function is suppose to have the user enter a name and a score and store them in 2 seperate arrays, one for the names, one for the scores. I believe I have to use pointers but have no idea on how to use them. I don't want the answer, just a push in the right direction. Here is the code:
#include <iostream>
int InputData(int &, char, int);
using namespace std;
int main()
{
char playerName[100][20];
int score[100];
int numPlayers = 0;
InputData(numPlayers, playerName, score);
return 0;
}
int InputData(int &numPlayers, char playerName[][20], int score[])
{
while (numPlayers <= 100)
{
cout << "Enter Player Name (Q to quit): ";
cin.getline(playerName, 100, ā\nā);
if ((playerName[numPlayers] = 'Q') || (playerName[numPlayers] = 'q'))
return 0;
cout << "Enter score for " << playerName[numPlayers] <<": ";
cin >> score[numPlayers];
numPlayers++;
}
}
Ok, I made some more changes and the errors are less, must be getting close, Lol!
This looks like a school assignment and I applaud you for not asking for the answer. There are several ways to do it, but you are already fairly close in the approach that you are using. When you pass an array reference, you do not want to include the length of the array. For example, the parameter int score[100] should be int score[]. The exception, especially in your scenario, is with multidimensional arrays. In this case, you want to use char playerName[][20]. Your function declaration also needs to change to match. Don't forget InputData returns an int. Your declarations and function call are correct; you just need to adjust your function signature.
Keeping the errors aside -
InputData(numPlayers, playerName, score, size);
// ^^^^ size is no where declared
// resulting Undeclared indentifier error
Prototype mentions of taking 3 arguments but calling the function passing 4 parameters.
Hint regarding errors:
An 1D array decays to a pointer pointing to first element in the array while passing to a function.
A 2D array decays to a pointer pointing to the 1D array ( i.e., T[][size] ) while passing to a function.
Return type of main() should be int.
It seems with the given hints you corrected most of the errors. But you forgot to change the prototype. So, change -
int InputData(int &, char, int);
to
int InputData(int &, char[][20], int[]);
Why aren't you using std::string array for player names ? Use it and remove rest of the errors. Good luck.