-
new issue
Here's the program.
the goals are:
1. user inputs values
2. displayMatrix() displays array and its values
3. user inputs multiplier value
4. scalarMultiply() multiplies values by whatever user input was
5. displayMatrix() displays array and new values
i'm stuck on no. 4
Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
void displayMatrix(int *value){
int i;
printf("Here is your array: \n\n");
for(i=0;i<SIZE;i++){
printf("In element [%i] is %i\n\n",i+1,value[i]);}
}
int scalarMultiply(int value[],int mult){
int i,sum=0;
for(i=0;i<SIZE;i++){
sum=value[i] * mult;
}
return sum;
}
main(){
int givenVal[SIZE],matrix,i,multiplier;
for(i=0;i<SIZE;i++){
printf("enter values for array: ");
scanf("%i",&givenVal[i]);
}
displayMatrix(givenVal);
printf("Enter value of multiplier: ");
scanf("%i",&multiplier);
matrix=scalarMultiply(givenVal[SIZE],multiplier);
displayMatrix(givenVal);
system("pause");
}
It runs, however when it gets to the part that the user enters the value to be multiplied by, it says "Unhandled exception at 0x00411443 in matrix.exe: 0xC0000005: Access violation reading location 0xcccccccc." then when i got to check out the problem, it shows it happening at "sum=value[i] * mult;" and the error message there says,"error: expression cannot be evaluated"(it shows a value for "sum" and "mult" in the output under the compiler, but "value[i]" has the latter error message.)
please help!
-
You should not be passing the array this way:
Code:
matrix=scalarMultiply(givenVal[SIZE],multiplier);
Since that would mean element 10 of givenVal, which givenVal has 10 elements, but they are numbered 0-9.
Remove the red part.
-
oooooohhhh!!! got it thanks! how do i display the newly multiplied values using displayMatrix()?
-
With a very similar for loop, like you used to enter the values into the matrix, originally.
Instead of a scanf() line of code, you will use a printf("%d ", arrayName[i]); line of code.
displayMatrix() is a very democratic function. It won't discriminate against any values in the matrix, at all. Just send it the matrix, and it will do the work.