Thread: Array help

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    21

    Array help

    Hi! I am currently working on a small exercise in C that I am kinda stuck on.

    It says: Write a program that reads a sequence of 8 integer numbers into an integer array, finds the minimal value in the array and prints the elements of the array from the min number to the end of the array. (*Note that the min value can be anywhere - even at the beginning or end of the array. If there is more than one element with the same min value, the first has to be considered.)

    Sample Input:

    1 3 9 9 2 0 0 1

    Sample Output:

    0 0 1

    The code I currently have is as follows:

    Code:
    
    #include <stdio.h>
    #define N 7 /* dimension of the array */
    
    void main(){
    
    	/* declare an array */
    	int ar[N]; /* elements from ar[0] to ar[N-1]; */
    	int i, min;
    	
    	/* array input */
    	for (i = 0; i < N; ++i) {
    		printf("%d> ", i);
    		scanf("%d", &ar[i]);
    	}
    
    	/* finding min of array elements */
    	min = ar[0];
    	for (i = 1; i < N; ++i) 
    		if (min > ar[i]) min = ar[i];
    
    	printf("%d\n", min);
    
    }
    Can anyone please help me with that last part, the printing of the array integers to the end of the array?

    Thank you so much.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) 'void main' is wrong. The function 'main' always returns an int. Always.

    2) The problem...

    Is quite simple. Loop through the array to find the smallest. Once you have, loop through the array again, until you find that number. Once you do, output that number, and the whole rest of the array.

    The easy way to do this is to break out of the loop once you find the low number (when you're actually searching for it, once you know what it actually is). Then start a new loop from that point.

    You could do this in one shot, by recording where in the loop the low number is when you find it the first time. This would save you the trouble of having to loop again for it. Then just start a new loop from that point.

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

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    21
    Hey thanks for replying so quickly!

    I understand what you mean by recording the location, I just can't figure out how (that's how pathetically new I am to this language).


    So after I have that first loop to find the min, should I not have min become ar[i]? Or should I just start the next loop with something like

    Code:
    if (min = ar[0])
        printf("%d, %d, %d", &ar[0], &ar[1], &ar[2]);
    etc? Sorry for my slowness.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by deedlit
    Hey thanks for replying so quickly!

    I understand what you mean by recording the location, I just can't figure out how (that's how pathetically new I am to this language).


    So after I have that first loop to find the min, should I not have min become ar[i]? Or should I just start the next loop with something like

    Code:
    if (min = ar[0])
        printf("%d, %d, %d", &ar[0], &ar[1], &ar[2]);
    etc? Sorry for my slowness.
    Look at your first example:
    Code:
    /* finding min of array elements */
    	min = ar[0];
    	for (i = 1; i < N; ++i) 
    		if (min > ar[i]) min = ar[i];
    
    	printf("%d\n", min);
    In this case, you can use this code, except to record the location where 'min' is located, you'd use another variable to hold it, and put the value of 'i' in it.

    That way later when you wanted to find the min again, you'd just do:
    Code:
    for( i = value_of_i_from_last_loop; i < size_of_array; i++ )
        ...do whatever, print a[i] or whatever...
    Now your code here has a few issues also:
    Code:
    if (min = ar[0])
        printf("%d, %d, %d", &ar[0], &ar[1], &ar[2]);
    The first one is the if check. You use '=' to assign a value, and '==' to test a value. Otherwise what happens here is that 'min' is assigned 'ar[0]' when the if check happens. Since I doubt that's what you want, you'd use '==' to test for equality.

    Second is your printf. You only use the & symbol, in reference to addresses, when you want the address of a variable. In this case, you don't want that, because that would print the address of 'ar[0]', and you want what 'ar[0]' contains, so you'd leave the & off.

    The reason you use the & when you use scanf, is when you have a non-pointer variable that you're trying to read into.

    Scanf expects you to pass it pointers, so if you don't have a pointer directly, you can use the & operator to give the address of the variable you want to put the value into.

    If the terminology confuses you, a pointer is a variable that points to another one. That is, it holds the address of another variable, rather than a value of a variable.

    'min' holds the value of something from your array.

    '&min' would give you where 'min' is located in memory. Otherwise known as its address.



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

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    21
    Thanks very much. I see now what you mean, I have crazy syntax everywhere.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM