Thread: Urgent help needed!!!

  1. #1
    Registered User
    Join Date
    Sep 2002

    Exclamation Urgent help needed!!!

    Write a program that produces 20 random numbers between 1 and 20. The program should store all nonduplicate values in an array.
    Use the smallest possible array to accomplish this task.

    I can get the numbers generated no problem however, i can't get my program to check against the array to se if the value has been stored or not...

    Here is my code thus far:
    #include <stdlib.h>
    #include <stdio.h>
    #define val 20	/*variable to be stored in the array*/				
    int main()
    	int i,x;
    	int array[val];
    	for ( i = 1; i <= 20; i++ ) 
    		x = 1 + (rand() % 20);
    		printf("%d", array[val]);
      return 0;
    [edit]Code tags added by Hammer.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    First off, read this, then rethink your thread titles for next time

    Also, check the code tags if you want to post code.

    Some pointers for your coding...

    This is outside of the array bounds. Remember you access the array by going 0 to val-1, not 1 to val. Also, I'm guessing here, but maybe you meant:
    >array[i] = x;
    .. and the same thing on the printf() statement.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Join Date
    Aug 2001
    Groningen (NL)
    >i can't get my program to check against the array to se if the value has been stored or not...

    You could use a loop within the loop for this to check if the value is already present in the array. Don't forget that if you have read in only N values, you must check only the first N values, since a duplicate value might be present after N. Such looks like this.

    for (n = 0; n < MAX; n++)
        value = get_value ();
        /* Check if value already in array */
        duplicate_found= false;
        for (m = 0; m < n; m++)
            if (array [m] == value)
                duplicate_found = true;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Urgent Help Needed In Writing Algorithm!!
    By Vikramnb in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2009, 12:46 PM
  2. Urgent Needed - PERL
    By beely in forum A Brief History of
    Replies: 2
    Last Post: 07-02-2002, 08:09 PM
  3. Help Needed: Borland C++ 5.5 Installation - URGENT!
    By Linette in forum C++ Programming
    Replies: 12
    Last Post: 03-09-2002, 06:44 PM
  4. Urgent Help Needed
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 07:48 PM