Thread: Length of an array

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

    Length of an 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
    Registered User
    Join Date
    Dec 2015
    Posts
    112
    See if this works for you. I got rid of the do while loop and made the variable names a bit clearer.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int main()
    {
        srand(time(NULL));
        int numToGuess = rand()%10;
        int guessCount, isGuessRight, currentGuess;
    
    
        isGuessRight = 0;
        guessCount = 0;
    
    
        while(!isGuessRight)
        {
            printf(" Enter a number.\n");
            scanf("%d",&currentGuess);
    
    
            if(currentGuess < numToGuess)
                printf("Enter a bigger number.\n");
            else if(currentGuess > numToGuess)
                printf("Enter a smaller number.\n");
            else
            {
                printf("Congrats you won.\n");
                isGuessRight = 1;
            }
    
    
            guessCount++;
        }
    
    
        printf("You had to guess %d times to win!\n", guessCount);
        system("pause");
    
    
        return 0;
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,266
    You have a problem right here:
    Code:
    int i,n=0,tip=0;
    int array[tip];
    You initialised tip to 0, then you tried to create a variable length array of tip, i.e., 0, number of elements. I don't remember offhand if this is permitted, but if it is, you end up with an array that you cannot index since it has no elements. If it isn't permitted, well, then it isn't permitted so you would get an error (or maybe undefined behaviour).

    Anyway, for this problem, you don't need an array, whether of fixed size, a variable length array, or a dynamic array. Rather, you need:
    • a variable to store the number to be generated by the RNG
    • a variable to store the guess of the user
    • a variable to store the count of the number of guesses

    All these variables can be of integer type.

    Note that you also appear to be using another array or pointer named tomb which was not declared, which makes me wonder if you tried copying code from elsewhere without understanding it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jul 2016
    Posts
    3
    Thank you very much!
    I wasn't copying i just tried this in my own language and i rewrote it to english, so it would be better to read it for others, but i was too tired already, and made a little bit of mess of it. But thank you again it helped me to learn!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. str length of int array
    By Sorinx in forum C Programming
    Replies: 12
    Last Post: 11-24-2012, 07:44 AM
  2. 2D Array length
    By otaconBot in forum C Programming
    Replies: 4
    Last Post: 08-08-2012, 03:08 PM
  3. c++ array length?
    By corbinc in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2009, 02:11 PM
  4. array length
    By Wick in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2003, 04:53 PM
  5. length of an array
    By chrismiceli in forum C Programming
    Replies: 8
    Last Post: 02-27-2003, 11:18 PM

Tags for this Thread