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 1 year ago.
Improve this question
i got a 5x5 matrix and i need to hash its values.
so i'm trying to hash the 2nd lines and 3rd columns like this
void hashing(int matris[5][5]) {
int x = 0;
int y = 0;
cin >> x;
cin >> y;
for (int i = 0; i < 5; i++)
{
for (int k = 0; k < 5; k++)
{
if ((i==x-1)|| (k==y-1))
{
matris[i][k] = "*";
}
}
}
}
thats the code but i got a error that follows:
"a value of type "const char*" can not be assigned to an entity of type "int""
does anyone knows how to do it ?
When you declare int matrix[5][5] you are telling your program that you want to store integers in that matrix, basically numbers. So the error says that your can't put into a matrix that stores integers, a char like "*", and infact you can't.
Edit: a possible solutions will be changing your matrix from int to string (parsing integers to strings) as #Some Programmer Dude suggested, but that depends on your needs
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
I'm working on a hangman game for a college asignment and i aleady have most of the work done but for some reason this part here is not working. Is there a problem with my logic? It seems to be incredibly simple.
bool second_check(char user_input) {
char u[3]={'a','r','i'};
for (int i = 0; i <= 3; i++) {
if (user_input==u[i]){
return true;
};
};
return false;
}
int main(){
char o;
cout<<"enter"<<endl;
cin>>o;
if (second_check(o)==true) {
cout << "Correct!" << endl;
}
else
cout << "Wrong! \n Strike one!" << endl;
return 0;
}
The for loop will loop 4 times even though you have 3 elements, causing it to try and access an undefined location in memory, to fix this replace the 'i <=3' with 'i<3'
so the for loop should like this in the end:
for (int i = 0; i < 3; i++) {
if (user_input==u[i]){
return true;
};
};
Answer
Since char u[3]={'a','r','i'}; contains only 3 characters, your for loop will be:
for (int i = 0; i <= 2; i++) or
for (int i = 0; i < 3; i++).
Explanation
This is because, in C/C++ and most of the programming language, the array count starts from 0. Therefore the first element will be array[0] and last will array[n-1] where n is the size of the array used at initialization. (Above, n=3)
So it would be helpful to the community if you state clearly what the problem is (i.e expected vs. actual output).
That said, a couple problems I can see...
for (int i = 0; i <= 3; i++)
if (user_input==u[i]){
Since u is of size 3 (char user[3]), you need to change your for loop to be i < 3 since arrays are 0 based, valid indices are 0,1,2 and you will go out of bounds of the array. I.e. user[3] is not a valid index.
You are not comparing an index of user_input which I suspect you want to. i.e. user_input[i].
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Working on a project that reads integers from a file and puts them into a 2D array. I tested it with a 1D array but when I tried it with a 2D array I kept getting this error "invalid types ‘int[int]’ for array subscript" in my class called "Image" in this function:
void Image::read(int* arr)
{
//Nested loop that reads the file
for(int i = 0; i < height; i++)
{
for(int k = 0; k < width; k++)
{
inputFile >> arr[i][k]; //Here's where I get the error
}
}
}
And here is my main function:
int main()
{
Image test("colorado1.dat"); //File with the integers
test.setWidth(500);
test.setHeight(500);
int array[test.getHeight()][test.getWidth()];
test.read(array);
//Loop to test if the function worked
for(int i = 0; i < 500; i++)
{
for(int k = 0; k < 500; k++)
{
cout << array[i][k] << " ";
}
}
}
Take a look at this function signature:
void Image::read(int* arr);
Here, you've said that the type of arr is int*, a pointer to an integer. As a result, when you write
arr[i][k]
C++ says "hold on a second... arr is an int *, so arr[i] is an int, and you can't apply the square bracket operator to a pair of int values."
To fix this, you'll need to change up the way that you handle parameters to this function. You could either pass in an int**, which would represent a pointer to a pointer to an int, or consider using something like std::vector<std::vector<int>>, which is safer and easier to use.
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
#include <iostream>
using namespace std;
int n;
void displaysum(double mat[n][n])
{
double sum= 0;
for(int j=0;j<n;j++)
sum += mat[j][j];
cout<<"Sum of Diagnols Elements is \n"<<sum;
}
int main()
{
cout << "what are the number of rows or column in the matrix" << endl;
cin >> n;
double matrix[n][n];
for (int row = 0; row < n; row++)
{
for (int column = 0; column < n; column++)
cin >> matrix[row][column];
}
displaysum(matrix)
return 0;
}
I don't understand why I get an error for no matching function to call inXCODE. Even if I try to change the variable in my function prototype it still gives me the same error.
I don't understand why I get an error for no matching function to call inXCODE.
The basic issue is that C++ wants the second dimension to be constant at compile-time for the sake of type-checking. If you want to get around this you'll have to use pointers (AFAIK). You can make this work by changing the declaration of displaysum to
void displaysum(double **mat)
and making appropriate allocations for matrix in the original function.
If you don't like this, well, welcome to C++'s type system. In the function declaration, double mat[n][n] is seen as double (*)[n]. This actually makes sense, but why it doesn't see matrix as being of that type is because n isn't constant. You can change the call
displaysum(matrix);
to this:
displaysum(static_cast<double (*)[n]>(matrix);
and receive the curious error
static_cast from 'double (*)[n]' to 'double (*)[n]' is not allowed
(and that's not the strangest error you'll get out of the type system)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
My code is below but there is some error can anyone guide me to write logic to return continuous numbers, for example, if array[] = {1,3,5,2,3,4,7,4,5,6} then function should return 2,3,4,4,5,6 keep time complexity in mind?
#include <stdio.h>
#define max 10
int coll[max];
void call_sort(int* p) {
int i = 0, first, sec;
while (*p) {
first = *p;
p++;
sec = *p - 1;
if (first == sec) {
coll[i] = *p;
i++;
}
int j;
for (j = 0; j < max; j++) {
printf("%d ", coll[j]);
//coll=coll+1;
}
}
}
int main(void) {
printf("ya\n");
int buff[max], i;
for (i = 0; i < max; i++)
scanf("%d", buff[i]);
call_sort(&buff);
}
There are many problems with your code
Wrong parameter to scanf(), you need to pass the address of the variable to modify it inside scanf() like this
scanf("%d", &buff[i]);
and you should also check that scanf() did read the value correctly by checking it's return value.
Wrong parameter to call_sort(), this will not cause any problem in this case, but it's wrong, and this combined with the scanf() issue, means that you did not enable compiler warnings, you should.
The correct way to pass buff is simply
call_sort(buff);
Wrong while (*p) which assumes that 0 is the last element of the array.
You should probably pass the size as a parameter and write a for loop, since you are dealing with numbers and 0 is a number, if it were a text string then it would be ok to exclude 0 from the normal values and use it as a sentinel value which is done in the standard library functions for strings.
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 8 years ago.
Improve this question
I am trying to copy data from one vector to another but am getting an error , "Invalid operands to binary expression 'int' and 'Card' " when I try to compile the following for loop:
for (int i = 0; i <= vectorOne[vecCapacity]; i++) { //step 3
vectorTwo[i] = vectorOne[i];
}
Would anyone have any suggestions?
I believe what you meant is
for (int i = 0; i <= vecCapacity; i++)
or even more likely
for (int i = 0; i < vecCapacity; i++)
The error message is clear enough: in this loop
for (int i = 0; i <= vectorOne[vecCapacity]; i++) { //step 3
vectorTwo[i] = vectorOne[i];
}
i has type int while vectorOne[vecCapacity] has type Card and there is no defined operator <= for these types.
So this loop makes no sense.
Maybe you mean
for (int i = 0; i < vecCapacity; i++) { //step 3
vectorTwo[i] = vectorOne[i];
}
Also take into account that you have to guarantee that the size of vectorTwo is not less than the size of vectorOne or at least vecCapacity.
You could use standard algorithm std::copy declared in header <algorithm>
For example
#include <algorithm>
//...
std::copy( vectorOne, vectorOne + vecCapacity, vectorTwo );
You should be looping from 0 to vectorOne's size.
for (int i = 0; i < vectorOne.size(); i++) { //step 3
vectorTwo[i] = vectorOne[i];
}`
`
Also, if you're doing it this way, make sure vectorTwo is big enough before the loop.
vectorTwo.resize(vectorOne.size());