iasteele

 

Varun

Page history last edited by Anonymous 2 yrs ago

C++ Evaluation

 

Variable types: int, float, char. They are used within functions and loops to make the final product. For example, if you were carrying out arithmatic functions, you could use the int variable to assign numerals for the functions.

 

printf: A code if u want to print something on page that can be seen. Anything you put in the quotes of the printf function will appear on the page.

 

scanf: A code if you want user to be able to type in something on the page. If you were to ask a question using the printf function, scanf would be used to allow the user to type in their answer.

 

Loops (ex. while and for): Codes used to make a certain action repeat again and again. If you were creating a game, you could use a loop to allow the user to play the game continuously until they win.

 

Arrays: The number of variables of a certain type. Arrays are used as parameters to certain variables that define the quantity of the certain variable.

 

Functions: A code that is already working and is refered to by a single command ex. printf. Functions are precreated and can be used to carry out complex task with just a single command.

 

if/else: If and else functions are used to set up conditions. The if function is used to make a proposition. If the proposition is true, the functions under the if function will be carried out. Otherwise, the else function will be carried out.

 

Here is a sample code:

 

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

 

void main()

{   

    int r;                                                  Here are the variables.

    int score = 0;

    int guess = -1;

    char return_key;

   

    srand((unsigned int)time(0));

    r = rand() % 100;

 

    while( guess != r)                           This is a loop.

    {

        printf("Enter a number between 0 and 99.\n");

        scanf("%i%c", &guess, &return_key);

        score = score + 1;

        if(guess == r )                                                                       Here is an if function.

        {

            printf("Correct! Your score is %i", score);

        }

        if(guess != r)

        {

            printf("Wrong!\n");

            if(guess>r)

            printf("Guess is greater than number.\n");

            if(guess<r)

            printf("Guess is less than number.\n");

        }

    }

 

   

   

//    printf("r = %i",r);   

    while(1);

}

 

 

 

 

 

This code is that of a game. You assign a random number and ask the user to guess that number. Then, you tell the user if they guessed the right number and if not, you let them keep trying. You also tell them their score when they win. It is amazing how this random looking code creates a fun game.

 

Comments (0)

You don't have permission to comment on this page.