Angela
Angela

Reputation: 41

Need help with arrays and loops

Use a single-subscripted array to solve the following problem: Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print it only if it is not a duplicate of a number already read. Provide for the "worst case" in which all 20 numbers are different. Use the smallest possible array to solve this problem.

here is what I have so far:

#include <stdio.h>
#define SIZE 20

int duplicate (int num[] );
int main ()
{
    int i, numbers[ SIZE ];
    printf( " Enter 20 numbers between 10 and 100:\n " );
    scanf_s( "%d\n" );

    for (int i = 0; i < SIZE  - 1; i++ ); 
    {


int duplicate( int num[] )
{
    int i, hold;

    for ( i = 0; i <= SIZE - 1; i++ )
        if ( num[i] == num[i=1] ){
            hold = num[i];
            else
                hold = num[i+1];
        }

    printf( "%3d\n," num[ i ] );
}

Upvotes: 1

Views: 3463

Answers (4)

Angela
Angela

Reputation: 41

This is what I came up with, thanks for everybody's help:

  #include <stdio.h>
  #define MAX 20

  int main()
  {
int a[ MAX ] = { 0 };  /* user input */
int i;                 /* counter */
int j;                 /* counter */
int k = 0;             /* number of integers entered */
int duplicate;         /* notify of duplicates */   
int value;              

printf( "Enter 20 numbers between  10 - 100;\n" );

      /* ask user for 20 numbers */
      for ( i = 0; i <= MAX - 1; i++ ){

    duplicate = 0;
    scanf( "%d", &value);

     /* decide if integer is duplicate */
    for ( j = 0; j < k; j++ ) {

     /* notify and stop loop if duplicate */
        if ( value == a[ j ] ) {
        duplicate = 1;
        break;
        { /* end if */



     /* enter number into array if it's not a duplicate */
        if ( !duplicate )
        a[ k++ ] = value;

} /* end if */

Upvotes: 2

Ben Voigt
Ben Voigt

Reputation: 283624

Your professor is, unfortunately, probably not smart enough to solve his own problem. The smallest possible array for this problem is size 2 (Assuming a 64-bit data type, which is the largest the standard provides for. With 32-bit integers it would need three elements, and with 128-bit integers, just 1).

#include <stdint.h>
#include <stdio.h>
int main(void)
{
    int_fast64_t visited[2] = { 0 };
    int inputs_left = 20;
    do {
        int input, slot;
        int_fast64_t mask;
        puts("Enter an integer between 10 and 100: ");
        if (!scanf("%d", &input)) {
            puts("That's not a number!\n");
            continue;
        }
        if (input < 10 || input > 100) {
            puts("Out of range!\n");
            continue;
        }
        slot = (input - 10) >> 6;
        mask = 1 << ((input - 10) & 0x3F);
        if (visited[slot] & mask) {
             puts("Already seen, it is a duplicate.\n");
        }
        else {
            visited[slot] |= mask;
            printf("%d is new\n", input);
        }
        inputs_left--;
    } while (inputs_left);
    return 0;
}

You are welcome to use this code in your assignment, if you are able to correctly explain how it works (I hope your professor taught you how to write comments).

Upvotes: 4

nnnnnn
nnnnnn

Reputation: 150010

My C is pretty rusty, so here's a pseudo-code solution (since this is homework you should do some of it for yourself):

print initial prompt;

declare nums[ array size 20 ]; // I later assume a 0-based index
declare boolean found;

for (i=0; i < 20; i++) {
  // prompt for next number if desired
  read next number into nums[i];
  found = false;
  // compare against all previously read numbers
  for (j=0; j < i; j++) {
    if (nums[j] == nums[i]) {
      found = true;
      break;
    }
  }
  if (!found) {
    print nums[i];
  }
}

Note: the question as stated doesn't say the numbers have to be integers. Also, it says "use the smallest possible array" - you could do it with a 19 element array if you introduce a non-array variable for the current number (since the 20th number read only needs to be checked against the previous 19, not against itself), but that makes the code more complicated.

See also the comment I posted above that mentions some specific things wrong with your code. And check that all of your brackets match up.

Upvotes: 0

Khasm
Khasm

Reputation: 163

There are a few problems with your code:

  • The duplicate function is inside the main function.
  • i is declared multiple times
  • There should not be a semicolon after your first for loop.
  • The hold variable is not being used for anything. It is only being assigned a value.
  • num[i=1] - not sure what you are trying to do here, but the i=1 is setting i to 1.
  • In your first for loop, your condition is i < SIZE - 1, meaning it will loop 19 times, not 20. It should be i < SIZE or i <= SIZE - 1.
  • Your if statements should use braces ({}) for each if/else, or not at all.

    if (test) {
        // code
    }
    else {
        // code
    }
    

    or

    if (test)
        // code
    else
        // code
    

As for the logic:

  • You are only getting one integer, which you are not putting in the numbers array. You will need to get 20 integers one by one and check the array each time the user enters a number.
  • The duplicate function should probably take a second parameter, the number that you want to check for. The if statement would check if num[i] equals the number you are looking for.
  • Remember to initialize the array values and only check values that you have set. For example, when the user enters the third number, you only want to check the first 2 numbers in the array to see if it already exists.

PS: Please try to indent your code properly. Many people will not even try to help if it is not indented properly.

Upvotes: 1

Related Questions