Thread: new issue

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    9

    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!
    Last edited by hiddenprophecy; 04-15-2009 at 09:43 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    9
    oooooohhhh!!! got it thanks! how do i display the newly multiplied values using displayMatrix()?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.
    Last edited by Adak; 04-15-2009 at 06:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  2. type safe issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 09:32 PM
  3. directsound issue
    By valis in forum Tech Board
    Replies: 0
    Last Post: 06-25-2006, 09:28 PM
  4. Replies: 8
    Last Post: 01-17-2006, 05:39 PM
  5. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 04:02 PM