Thread: simple C program help

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    2

    simple C program help

    I'm studying for a programming class where were learning C, and I'm doing a practice problem that I can't get right. I feel like it should be easy but somethings wrong. The program is just to use a given array and find whether or not theres a repeat in it. If there is the value of the repeat should be displayed, if not -1 should be. Heres my code:

    Code:
    #include<stdio.h>
    
    
    int firstRepeat(int* input, int size);
    
    int main() {
        int n = 5;
        int myArray[5] = {1,2,3,4,4};
        int fRepeat;
    
    
        fRepeat = firstRepeat(myArray, n);
    
    
        printf("First repeating integer: %d\n", fRepeat);
    
    
        return 0;
    }
    
    int firstRepeat(int* input, int size) {
        int i, test, value;
    
    
        input=&i;
    
    
        for(i=1; i<size; i++) {
            test = *input - *(input-1);
            if(test == 0) value = *input;
            else value = -1;
        }
        return value;
    }
    The function needs to use int* input and int size, so the function needs to work similar to how I have it. My problem is clearly it should display the repeat to be 4, yet i get -1 each time. I'm pretty stuck so any help is appreciated. Thanks in advance

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Welcome to the forum! Congrats on posting properly indented code on your first try, few forum newbies get that right .


    Also, good effort on using a named value to represent the size of the array, but you should use it "everywhere". I put "everywhere" in quotes because you want to use the 'size' parameter inside the firstRepeat function. Also, pick a better name than 'n'.
    Code:
    #define ARRAY_SIZE 5
    ...
    int myArray[ARRAY_SIZE] = {1, 2, 3, 4, 4};
    fRepeat = firstRepeat(myArray, ARRAY_SIZE);
    Now, think about how you would do this yourself. Your example array is short, so a human could just look at it and get it. But imagine if there were 100 elements. Harder, right? Now, how would you do that by hand? Hint: you can't just look at each element once (i.e. one for loop is not enough). You need to look at the first element and compare it to all others. Then, look at the second element...


    Does that give you some hints as to the code structure?

  3. #3
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    Your statement: input = &i in firstRepeat() makes that input is pointing to an irrelevant value; it no longer points to "myArray" so firstRepeat() will never "know" the content of myArray

  4. #4
    Registered User
    Join Date
    Nov 2013
    Posts
    2
    Thank you anduril462, I got it working correctly now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  2. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  3. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  4. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM
  5. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM

Tags for this Thread