Finding and returning a common value from an array of 8 integers

This is a discussion on Finding and returning a common value from an array of 8 integers within the C Programming forums, part of the General Programming Boards category; Hello all, I am new to this forum and C Programming. One of my hw assignments is to write code ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    1

    Wink Finding and returning a common value from an array of 8 integers

    Hello all,

    I am new to this forum and C Programming. One of my hw assignments is to write code that returns a value that is repeated more than once in an array of 8 integers.

    For example:

    Numbers: 30 12 100 33 2 2 1 40
    Output: 2

    Here is my code so far:




    Code:
    #include <stdio.h>
    
    int main(void){
      int i;
      int j;
      int arr[8];
      int count;
      int maxCount = 0;
      int maxValue = 0;
    
      printf("Enter 8 values between 1-100: ");
    
      for(i = 0; i < 8; i++){
        scanf("%d", arr[i]);                                  /* ERROR */
      }
    
      for(j = 0; j < 8; j++){
        count = 0;
        for(i = 0; i < 8; i++){
          if(arr[i] == arr[j]){
            count++;
          }
          if(count > maxCount){
            maxCount = count;
          }
        }
    
        maxValue = arr[i];
      }
    
      printf("Number: %d", maxValue);
    }
    When I compile this code (on Vim), I get an error when I print the array of 8 above (see ERROR above).

    Thanks for your help.

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    The argument you give to scanf must be a pointer to a location that you want to store the read data to. arr[i] is an integer, not a pointer. You can get the pointer to arr[i] with the address-of operator &.

    Code:
        scanf("%d", &arr[i]);
    You always need to put & in front of any non-pointer variables you pass to scanf.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    In your loop to find repeated values, you will get false hits since arr[i] is always equal to itself.

    What is supposed to happen if there are two distinct repeated values in the array? For example 30 12 12 30 10 11 13 14
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    Quote Originally Posted by ejoshi View Post
    When I compile this code (on Vim), I get an error when I print the array of 8 above (see ERROR above).

    Thanks for your help.
    1. vim is an editor, not a compiler.
    Your compiler is most likely gcc.

    2. You need to say more than "ERROR".
    An actual error message would be
    foo.c:8:3: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘unsigned int’ [-Wformat]
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding a common suffix
    By echo1525 in forum C Programming
    Replies: 3
    Last Post: 11-12-2012, 12:35 AM
  2. finding common numbers
    By BB89 in forum C Programming
    Replies: 14
    Last Post: 11-10-2009, 08:22 AM
  3. finding most common char in a string
    By scwizzo in forum C++ Programming
    Replies: 6
    Last Post: 11-23-2007, 01:22 PM
  4. C program for finding highest common factor!!
    By visham in forum C Programming
    Replies: 11
    Last Post: 08-02-2007, 08:10 AM
  5. Finding the most common char
    By Mikro in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 09:00 AM

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