Thread: elements in array

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    3

    elements in array

    Hey everyone!

    I need a little help with my code... I have made a simple game where you have to guess the number, which has been created by the RNG, and it helps you to get closer to the number by saying : Choose a bigger number / choose a smaller number... and when you finally enter the right number it says : Congrats , you won. But then i would like to know in the end of the programm, that how many times you had to guess to get the correct number. Here's what i have been trying to do, here is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int main()
    {
        srand(time(NULL));
        int x = rand()%10;
        int i,n=0,tip=0;
        int array[tip];
    
        do {
          printf(" Enter a number.\n");
          scanf("%d",&array[tip]);
            if (array[tip]<x) {
                printf("Enter a bigger number.\n");
                } else if (array[tip]>x) {
                    printf("Enter a smaller number.\n");
                    } else {printf("Congratz, you won!\n");
                    }
            }while(array[tip]!=x);
    
           for(i=0;i<10;i++)
                if(tomb[tipp]=x) {
                 n= sizeof(tomb)/sizeof(tomb[i]);
                   }
                  printf("You had to guess %d times to win!\n", n);
    
    
        return 0;
    }

    Please help me if you can!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Duplicate
    Length of an array
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Add elements into an array in C
    By Marria in forum C Programming
    Replies: 6
    Last Post: 09-21-2012, 04:19 PM
  2. Replies: 2
    Last Post: 12-07-2011, 11:22 PM
  3. Sum of all elements in an array
    By SilentPirate007 in forum C Programming
    Replies: 6
    Last Post: 05-02-2010, 08:32 AM
  4. Sum all odd array elements?
    By Marth_01 in forum C Programming
    Replies: 9
    Last Post: 11-04-2008, 10:47 PM
  5. array of zero elements
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 02-17-2008, 07:10 AM

Tags for this Thread