error: array subscript is not an integer - c

int row,col,i,j,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6,tmp7;
if(M==64&&N==64){
for(row=0;row<N;row+=8){
for(col=0;col<M;col+=8){
for(j=0;j<2;j++){
for(i=row;i<row+4;i++){
tmp0=A[i+4j][col+0];
tmp1=A[i+4j][col+1];
tmp2=A[i+4j][col+2];
tmp3=A[i+4j][col+3];
tmp4=A[i+4j][col+4];
tmp5=A[i+4j][col+5];
tmp6=A[i+4j][col+6];
tmp7=A[i+4j][col+7];
B[col+0+4j][i]=tmp0;
B[col+1+4j][i]=tmp1;
B[col+2+4j][i]=tmp2;
B[col+3+4j][i]=tmp3;
B[col+0+4j][i+4]=tmp4;
B[col+1+4j][i+4]=tmp5;
B[col+2+4j][i+4]=tmp6;
B[col+3+4j][i+4]=tmp7;
}
but I got the code the error: array subscript is not an integer. But I don't know why.
Could someone have a look at and tell me why?
I do not think I use other type in the array besides int.

C does not support implicit multiplication like you commonly see in math. It obviously wouldn't work, since variable names can be more than one letter, it would create a huge amount of parsing confusion.
By your (implied) logic, an expression such as row < N should then be the same as r * o * w < N, which is clearly not what you really think.
Thus, multiplication must always be done explicitly using the * binary operator: 4j is a parse error, you meant 4 * j.

int row,col,i,j,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6,tmp7;
if(M==64&&N==64){
for(row=0;row<N;row+=8){
for(col=0;col<M;col+=8){
for(j=0;j<2;j++){
for(i=row;i<row+4;i++){
tmp0=A[i+4*j][col+0];
tmp1=A[i+4*j][col+1];
tmp2=A[i+4*j][col+2];
tmp3=A[i+4*j][col+3];
tmp4=A[i+4*j][col+4];
tmp5=A[i+4*j][col+5];
tmp6=A[i+4*j][col+6];
tmp7=A[i+4*j][col+7];
B[col+0+4*j][i]=tmp0;
B[col+1+4*j][i]=tmp1;
B[col+2+4*j][i]=tmp2;
B[col+3+4*j][i]=tmp3;
B[col+0+4*j][i+4]=tmp4;
B[col+1+4*j][i+4]=tmp5;
B[col+2+4*j][i+4]=tmp6;
B[col+3+4*j][i+4]=tmp7;
}

tmp0=A[i+4j][col+0];
It should be
tmp0=A[i+4*j][col+0];
replace at all places

Multiplication should be called explicitly: in your case, 4*j instead of 4j.

Related

Recursive function for finding factorial of a number

I am getting an output of 24 which is the factorial for 4, but I should be getting the output for 5 factorial which is 120
#include <stdio.h>
int factorial(int number){
if(number==1){
return number;
}
return number*factorial(--number);
}
int main(){
int a=factorial(5);
printf("%d",a);
}
Your program suffers from undefined behavior.
In the first call to factorial(5), where you have
return number * factorial(--number);
you imagine that this is going to compute
5 * factorial(4);
But that's not guaranteed!
What if the compiler looks at it in a different order?
What it if works on the right-hand side first?
What if it first does the equivalent of:
temporary_result = factorial(--number);
and then does the multiplication:
return number * temporary_result;
If the compiler does it in that order, then temporary_result will be factorial(4), and it'll return 4 times that, which won't be 5!. Basically, if the compiler does it in that order -- and it might! -- then number gets decremented "too soon".
You might not have imagined that the compiler could do things this way.
You might have imagined that the expression would always be "parsed left to right".
But those imaginations are not correct.
(See also this answer for more discussion on order of evaluation.)
I said that the expression causes "undefined behavior", and this expression is a classic example. What makes this expression undefined is that there's a little too much going on inside it.
The problem with the expression
return number * factorial(--number);
is that the variable number is having its value used within it, and that same variable number is also being modified within it. And this pattern is, basically, poison.
Let's label the two spots where number appears, so that we can talk about them very clearly:
return number * factorial(--number);
/* A */ /* B */
At spot A we take the value of the variable number.
At spot B we modify the value of the variable number.
But the question is, at spot A, do we get the "old" or the "new" value of number?
Do we get it before or after spot B has modified it?
And the answer, as I already said, is: we don't know. There is no rule in C to tell us.
Again, you might have thought there was a rule about left-to-right evaluation, but there isn't. Because there's no rule that says how an expression like this should be parsed, a compiler can do anything it wants. It can parse it the "right" way, or the "wrong" way, or it can do something even more bizarre and unexpected. (And, really, there's no "right" or "wrong" way to parse an undefined expression like this in the first place.)
The solution to this problem is: Don't do that!
Don't write expressions where one variable (like number) is both used and modified.
In this case, as you've already discovered, there's a simple fix:
return number * factorial(number - 1);
Now, we're not actually trying to modify the value of the variable number (as the expression --number did), we're just subtracting 1 from it before passing the smaller value off to the recursive call.
So now, we're not breaking the rule, we're not using and modifying number in the same expression.
We're just using its value twice, and that's fine.
For more (much more!) on the subject of undefined behavior in expressions like these, see Why are these constructs using pre and post-increment undefined behavior?
How to find the factorial of a number;
function factorial(n) {
if(n == 0 || n == 1 ) {
return 1;
}else {
return n * factorial(n-1);
}
//return newnum;
}
console.log(factorial(3))

Difference between declaration terminated incorrectly and declaration syntax error in C

I want to know when this difference happen Declaration terminated error and declaration syntax.
my code for Declaration terminated error:
int a=12,245;
and code for declaration syntax error:
int a=10 20 30;
You can say
int a = 12245;
and you're initializing a to the integer 12245.
You can say
double a = 12.245;
and you're initializing a to the floating-point decimal number 12.245.
If you want to confuse people, you might be able to say
int a = (12,245);
and this is like saying "I want to initialize a to 12, no, I mean 245". (a would end up holding 245).
But you can't say
int a = 12,245;
The comma character can be used in several different ways in C, but there are also several ways that it can not be used.
In C source code, you have to use the full stop . as a decimal point, and you can't use a comma to group numbers into thousands or lakh or anything.
What you can use commas for are to separate the arguments in a function call, or to declare several variables at once, or as the comma operator. But these are all quite different, and int a = 12,245; doesn't end up fitting any of them.
For completeness, here are argument-separating commas in a function call:
printf("%d %c %s %f\n", 1, '2', "three", 4.5);
Here are commas separating multiple declarations:
int a = 12, b = 245;
Here are comma operators:
for(i = 0, j = n; i < j; i++, j--)
Integer can not tape floating point values like 10,3, Integer can only store natural numbers like 180! If you want to use floating point values, use float or double
In C, there is no difference. Both programs fail to conform to the syntax of the language.
C does not specify the text of error or warning messages produced by a compiler. Most compilers make some effort to produce error messages which help the programmer understand what the error is. Some do better than others, but none is perfect.
Whatever compiler you are using, in its current version, happens to produce two different error messages for that code. How it decided which message to produce is something probably only the authors of that compiler could tell you. If you feel that one or the other error message is significantly misleading, you could file a bug report or a formal complaint, if the compiler you are using has some sort of user-support channel which allows you to do one of those things.

Wondering how to add arrays of vectors?

I need to add two vectors in the arrays together. For example my code should execute the vector = {3,6,9}.
I dont really know what I did wrong as I am still new to coding. So any help is appreciated!
void add_vectors( double vector1[3]={1,2,3},double vector2[3]={1,2,3},double
vector3[3]={1,2,3}, int n)
{
n=sizeof(vector1);
int i;
for(i=0; i>n; i++)
{
scanf("%f", &vector1[i]);
scanf("%f", &vector2[i]);
vector3[i]=vector1[i]+vector2[i];
}
printf (vector3[]);
Sorry for the bad formatting but it's my frist time using this site.
There are several mistakes here in the code:
First, sizeof() gives you a size of something in a memory (in bytes), which is probably not what you desire.
Secondly, i>n statement means that the loops will execute only while i > n! The first time i = 0, and n is a positive integer. That means that the loop will be skipped, since i is not larger than n.
Third, printf() does not work like this.
I explained you the second point; my first and third points are widely explained on the internet: try finding these answers yourself.

Can someone explain how this simple C character comparison function works?

I am very new to the C programming language. Im confused why the function takes pointers as arguments? From my understanding, it looks like the function returns the difference in character value of 2 characters, is this correct?
Heres the function:
int charcomp(char *x, char *y) { return *x - *y; }
Thanks in advance for any help!
There is a C library idiom that a 'generic' comparison function takes two pointers to objects to compare and returns an int result that indicates the following:
a negative result means the first object is less than the second object
a zero result means the two objects are equal
a positive result means the first object is greater than the second object
Since the negative and positive results don't have to be specific values (though -1 and 1 are commonly used), it's also a common idiom to use a subtraction to generate that result for numeric comparisons. That's what is happening here.
However, beware that subtracting two int values could lead to undefined behavior in certain situations where overflow could occur (that usually won't be the case when subtracting char types though). A simple if/else set of tests can be used to avoid overflow.
This will compare the first character of two C strings (pointers-to-char). If the characters are the same it will return 0, otherwise it will return non-zero. This can be used as a boolean comparator, as in:
if (charcomp("alice", "aarvark"))
print("they start with different letters")
else
printf("they start with the same letter");

Beginner type conversion

I am extremely new to programming in general, so please forgive my noobishness. I am trying to scale down the res[?] array in the function below. I know the problem that the res[?]*(multiplier/100) is creating a decimal answer instead of the required format which is an integer therefore I need to convert the result before it is plugged into the res[?].
I did think of turning res[] array into a double but I wasnt sure whether the initwindow(?,?) was compatible with integer values.
I am on mingw with code blocks. the linker and compiler has customized setting made by my professor. I am on Plain\basic C???
I tried to apply the techniques this website used about the truncating conversion. But doesn't seem to work. http://www.cs.tut.fi/~jkorpela/round.html
Debugger watcher shows that res[?] is equivalent to 0.
#include <stdio.h>
#include <graphics_lib.h>
#include <math.h>
int res[2]//contains a array which contains the vertical and horizontal detected resolution from main()
void function(res)
{
printf("Please note the lowest resolution available is 800x600\n");
printf("Please enter percentage ratio % below 100:");
scanf("%d",&multiplier);
res[1]=(int)(res[1]*(multiplier/100));
res[2]=(int)(res[2]*(multiplier/100));
blah blah blah blah.............
initwindow(res[1],res[2]) //from custom header that my professor made which initializes basic graphics window
return;
}
I'm assuming multiplier is an int to match the %d format.
multiplier/100 is guaranteed to be zero (if the user follows directions and provides a number less than 100). You can do (res[x]*multiplier)/100 to make sure the multiply happens first (you're probably okay without the parentheses, but rather than think about order of operations why not be explicit?)
The cast to int is unnecessary, because an int divided by another int is always an int.
Unless your professor has done some very interesting things, you should also note that a two-element array such as res would have elements res[0] and res[1], not res[1] and res[2].
you don't need to cast in this situation because the conversion is done implicitly . you only need a cast to assign the content of a variable to a variable of different type .
Looks like the multiplier variable is an int, so the result of multiplier/100 expression will be 0 in all cases.
Also, it's a good programming practice to check the validity of user's input.
You must declare multiplier before you use it:
int multiplier;
printf("Please note the lowest resolution available is 800x600\n");
printf("Please enter percentage ratio % below 100:");
scanf("%d",&multiplier);
And the index is staring from 0 not 1. And you should initialize res[0] and res[1] before using them. So:
res[0] = 800;
res[1] = 600;
And the division of multiplier by 100 will truncate to 0, try this without casting as it will be automaticly converted:
res[1]=(multiplier*res[0])/100;
res[1]=(multiplier*res[1])/100;

Resources