Thread: Median in array

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    13

    Median in array

    I have this programm where u put in an amound of numbers and the program calculates the median. But when i give in any number it says acces violation error?
    i can't figure out the problem
    Code:
    /* INCLUDES: */#include  <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <string.h>
    #include <math.h>
    
    
    
    
    /*DECLARATIONS*/
    int nGem(int);
    
    
    /* MAIN PROGRAM: */
    
    
    int main(void)
    {
       int n;
       int nAantal;
       int nB;
       int nInvoer;
       int nArray[20]={1};
       printf("Hoeveel weerstanden wilt u invoeren?");
       scanf("%d", &nAantal);
       for (nB = 0; nB<nAantal; nB++)
       {
           printf("Voer een aantal weerstanden in");
           scanf("%d", nArray[nB]);
       }
    
    
       n = nGem(nInvoer);
       printf("%d", &n);
       return(0);
    
    
    }
    int nGem(int nInvoer)
    {
       	char chAgain = 'y';
    
    
       	int nAantal;
       	int nSum = 0;
    	int nAverage = 0;
       	int nB;
       	int nU ;
    	int nArray[100] = {1};
       
    	for (int i = 0; i < 10; ++i)
       	{
       		nSum+=nArray[i];
       	}
    	nAverage = nSum/10;
    	return nAverage;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
           printf("Voer een aantal weerstanden in");
           scanf("%d", nArray[nB]);
       }
    You missed the & there. array[X] gives you the value of spot X in array. If you want the address of that spot (and scanf does expect addresses as arguments), then you need to supply that.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    13
    Thnx for the help:P
    Okay, works for me, but there is a thing left. Any values i enter he calculates the same median: 1245008. I get that answer with any value i enter. Weird

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
     int nInvoer; /* what value does this have? */
       int nArray[20]={1};
       printf("Hoeveel weerstanden wilt u invoeren?");
       scanf("%d", &nAantal);
       for (nB = 0; nB<nAantal; nB++)
       {
           printf("Voer een aantal weerstanden in");
           scanf("%d", nArray[nB]);
       }
     
     
       n = nGem(nInvoer); /* what value are you passing to this function? */
    Now your function doesn't actually even use its argument, so you don't need to be passing it anything (even though you're passing it the wrong thing). This though is another problem:
    Code:
       printf("%d", &n);

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Make sure you do a check here for the number received so you don't go out of your array's boundary.

    Code:
    int nArray[20]={1};   
    printf("Hoeveel weerstanden wilt u invoeren?");
       scanf("%d", &nAantal);
       for (nB = 0; nB<nAantal; nB++)
       {
           printf("Voer een aantal weerstanden in");
           scanf("%d", &nArray[nB]);
       }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Median Help!
    By tidbit in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2010, 09:55 PM
  2. help with sorting of array and median!
    By Cali_engr in forum C Programming
    Replies: 2
    Last Post: 04-05-2010, 11:27 PM
  3. Median
    By Neo1 in forum C++ Programming
    Replies: 4
    Last Post: 07-02-2007, 04:00 PM
  4. Median
    By Suchy in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2006, 02:07 AM
  5. median
    By frank in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2001, 04:32 PM