C program HELP

This is a discussion on C program HELP within the C Programming forums, part of the General Programming Boards category; I need help on a program. Required: Write a program that counts how many elements in an array with N ...

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    17

    Post C program HELP

    I need help on a program. Required: Write a program that counts how many elements in an array with N elements are higher than the last element of the array. I need to use functions and the cycle for. I have done this but I can't understand what's wrong with it. Output array ex: 16 45 9 10--> 2 Numbers are higher than the last element.
    Code:
    # include <stdio.h>
    #include <conio.h>
    int number (int i, int n)
    {
        int v[n];
        int k=0;
        int b=sizeof(v)/sizeof(v[0]);
         for (i=0;i<n;i++)
        {
            if (v[i]>v[b-1])
            k+=1;
            }
            return k;
            }
    int main ()
    {
        int n,i,k;
        printf ("Give N: ");
        scanf ("%d", &n);
        int v[n];
        for (i=0;i<n;i++)
        {
            printf ("Give the %d element: ", i+1);
            scanf ("%d", &v[i]);
            }
        
        k=number(i, n);   
        for (i=0;i<n;i++)
            printf ("%4d", v[i]);
        printf ("\n");    
        
        printf("\n %d numbers are higher than the last element.", k);
        getch ();
        return 0;
    }
    Last edited by keivi; 06-09-2013 at 05:32 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    You need to pass your v array as a parameter to the function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    17
    how?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    Well since your homework covers arrays and functions, my guess is that passing arrays to functions was covered in class, or the preceding chapter of your book.

    I mean, there's a thread on this board, and on the front page with "the words 'arrays' and 'parameters' in it. Perhaps it's worth a read.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    17
    can you help me specifically in this code?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    Sure, but the whole point of the exercise (for you) is to learn how to do it for yourself.

    You need to at least show (by posting more code) that you've read the information available, and at least made an attempt at using it.

    We'll still be here if you get stuck.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jun 2013
    Posts
    17
    Thanks, I figured it out, here it is
    Code:
    # include <stdio.h>
    #include <conio.h>
    
    void number(int k)
    {   
        int i;
    	 int n;
        int v[100];
    
    	printf ("Give N: ");
        scanf ("%d", &n);
        
         for (i=0;i<n;i++)
        {
            printf ("Give the %d element: ", i+1);
            scanf ("%d", &v[i]);
            }
        
        for (i=0;i<n;i++)
            printf ("%4d", v[i]);
        
          k=0;
          
         for (i=0;i<n;i++)
        {
            if (v[i]>v[n-1])
           {
            k++;
            
            
           }
        }
        printf("\n %d numbers are higher than the last element.", k);
     
    }
        
        
    int main (void)
    {
        int n,i,k,v[100];
        
        number(k);
       
        getch ();
        return 0;
    }

  8. #8
    Registered User
    Join Date
    Apr 2013
    Posts
    1,322
    Your original program in your first post was very close. The main problem was that you did not pass v as a parameter. How to do this should have been explained in your class and/or your textbook. Change number() to

    Code:
    int number(int v[], int n)
    Where n is the number of elements in v.

    In main
    Code:
    /* ... */
        int v[100]
    /* ... */
        printf ("Give N: ");
        scanf("%d", &n);
        if(n > 100)   /* make sure n <= 100 */
            n = 100;
    /* ... */
        k = number(v, n);
    Last edited by rcgldr; 06-10-2013 at 09:05 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-11-2012, 12:25 AM
  2. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  3. Replies: 5
    Last Post: 08-17-2007, 12:43 AM
  4. Replies: 18
    Last Post: 11-13-2006, 01:11 PM
  5. How To Make The Program Installed In Program Files Folder?
    By javacvb in forum Windows Programming
    Replies: 4
    Last Post: 11-05-2003, 05:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21