Urgent help needed!!!

This is a discussion on Urgent help needed!!! within the C Programming forums, part of the General Programming Boards category; Write a program that produces 20 random numbers between 1 and 20. The program should store all nonduplicate values in ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    1

    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:
    Code:
    #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);
    		array[val]=x;
    		printf("%d", array[val]);
    		printf("\n");
    	}
      
      printf("\n");
      return 0;
    }
    [edit]Code tags added by Hammer.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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...

    >array[val]=x;
    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
    Location
    Groningen (NL)
    Posts
    2,386
    >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.

    Code:
    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, 11:46 AM
  2. Urgent Needed - PERL
    By beely in forum A Brief History of Cprogramming.com
    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, 05:44 PM
  4. Urgent Help Needed
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 06:48 PM

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