Question

In: Computer Science

Is there a shortcut or simpler way to get the same output in C ? #include...

Is there a shortcut or simpler way to get the same output in C ?

#include <stdio.h>

#include <time.h>

#include <stdlib.h>

#define OFFSET 1

#define RANGE 53

int RandomInteger(int, int);

int PrintInstructions();

int main()

{

//six integer variables

int a, b, c, d, e, f;

//one integer for num of sets

int numSets;

srand(time(NULL));

while (1)

{

int i;

numSets = PrintInstructions();

if (numSets == 0)

{

printf("* You have choosen to exit the application.\n");

break;

}

for (i = 1; i <= numSets; i++)

{

a = RandomInteger(RANGE, OFFSET);

do

{

b = RandomInteger(RANGE, OFFSET);

} while (b == a);

do

{

c = RandomInteger(RANGE, OFFSET);

} while (c == a || c == b);

do

{

d = RandomInteger(RANGE, OFFSET);

} while (d == a || d == b || d == c);

do

{

e = RandomInteger(RANGE, OFFSET);

} while (e == a || e == b || e == c || e == d);

do

{

f = RandomInteger(RANGE, OFFSET);

} while (f == a || f == b || f == c || f == d || f == e);

printf("* Set # %d of six numbers is: %d %d %d %d %d %d\n", i, a, b, c, d, e, f);

}

}

return 0;

}

int RandomInteger(int range, int offset)

{

int randomNum = rand() % RANGE + OFFSET;

return randomNum;

}

int PrintInstructions()

{

int numSets = 0;

printf("*************************************************************************\n");

printf(" Enter in the number of randomly generated sets of numbers you want or \n");

printf(" enter in a 'q' or 'Q' to quit. \n");

printf("*************************************************************************\n");

printf("* Your Input: ");

scanf("%d", &numSets);

return numSets;

}

Solutions

Expert Solution

Modified Program :

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define OFFSET 1
#define RANGE 53

int RandomInteger();
int PrintInstructions();

int main()

{
        //six integer variables
        int a, b, c, d, e, f;

        //one integer for num of sets
        int numSets;

        srand(time(NULL));

        while (1){
                int i;
                numSets = PrintInstructions();
                if (numSets == 0){
                        printf("* You have choosen to exit the application.\n");
                        break;
                }

                for (i = 1; i <= numSets; i++){
                        a = RandomInteger();
                        do
                        {
                                b = RandomInteger();
                                c = RandomInteger();
                                d = RandomInteger();
                                e = RandomInteger();
                                f = RandomInteger();
                        }while(b==a || c == a || c == b || d == a || d == b || d == c || e == a || e == b || e == c || e == d || f == a || f == b || f == c || f == d || f == e);
                
                        printf("* Set # %d of six numbers is: %d %d %d %d %d %d\n", i, a, b, c, d, e, f);
                }

        }       
        return 0;
}

int RandomInteger(){
        int randomNum = rand() % RANGE + OFFSET;
        return randomNum;
}

int PrintInstructions(){
        int numSets = 0;

        printf("*************************************************************************\n");
        printf(" Enter in the number of randomly generated sets of numbers you want or \n");
        printf(" enter in a 'q' or 'Q' to quit. \n");
        printf("*************************************************************************\n");

        printf("* Your Input: ");       
        scanf("%d", &numSets);

        return numSets;

}

Output :

Explanation :

  • As I see RandomInteger() function is defined to generate a Random number for user-defined range and offset. Here I have considered that RANGE and OFFSET are defined by users globally to use in the program.
  • There is no need to pass parameters in RandomInteger() function as in the main program RANGE and OFFSET are passed every time in the function call. Instead of passing them, They can be directly used to be used in the function definition as they are defined globally using the "#DEFINE" directive.
  • In the for loop, we have used 5 while loop to generate an integer which is not the same as the previous ones being generated.
  • Since all the previous depend on the "a" variable for comparing, it is kept same as it is defined.
  • A single while loop is used to generate all other variables and the conditions being used for them are inserted in this single while loop using the "||" (or) operator so that all the conditions should be satisfied i.e no integers being generated are the same.

Related Solutions

Sources are not all the same in the way they specify the output voltage. Some assume...
Sources are not all the same in the way they specify the output voltage. Some assume that a load with a resistance matching the output resistance is always present when you set the voltage. Other devices assume there is a high impedance load on the output. Which of the cases match the voltage settings for Elvis II? Explain your answer
The world would be simpler if the US Tax Code and US GAAP were the same,...
The world would be simpler if the US Tax Code and US GAAP were the same, and thus two sets of books would not be needed. Discuss the reasons they are not and whether or not you think it necessary for the present system to remain as it is
Can I please get a page-long response on the short story "Mrs. Todds Shortcut" try to...
Can I please get a page-long response on the short story "Mrs. Todds Shortcut" try to avoid summarizing the story and describe some of the literary elements. Thank You!!
Define the sample space. To get simpler expressions, you can prefer the set notations. (a) (6...
Define the sample space. To get simpler expressions, you can prefer the set notations. (a) (6 Points) What is the sample space of choosing a real number from interval [0, 1] which is larger than 0.5. Specify the properties of the sample space. Design two different random variables for this task. (b) (6 Points) You play rock-paper-scissors with your friend. The one with total three wins or two subsequent wins will be the winner of the game. Give a probability...
The major advantage of using structs is to make the C++ code simpler, easier to read,...
The major advantage of using structs is to make the C++ code simpler, easier to read, and easier to work with. After completing this assignment, students will be able to: • use structs to create and/or introduce new custom data types • implement structs to group fixed numbers of pieces of data of different types • implement structs within structs • chaining using the dot operators to access nested fields • copy an entire structure • create an array of...
Hi, I need to finish function purser,which will include header row and i can get same...
Hi, I need to finish function purser,which will include header row and i can get same output /* [ { firstName: 'moe', lastName: 'green' }, { firstName: 'lucy', lastName: 'liu' }, { firstName: 'ethyl', lastName: 'mertz' } ]*/ In javascript please const parser=(d)=>{ let arr=[]; let newArr=[]; let obj={} let items=d.split('|'); for (let i=0;i<items.length;i++){ arr[i]=items[i].split(','); } for (let z=0;z<arr.length;z++){ newArr=newArr.concat(arr[z]); } for(var y = 0;y < newArr.length;y+=2){ obj[newArr[y]]= newArr[y+1]; } //const res = newArr.reduce((a,b)=> (a[b]='',a),{}); return obj; } const data...
Please in C++ thank you! Please also include the screenshot of the output. I have included...
Please in C++ thank you! Please also include the screenshot of the output. I have included everything that's needed for this. Pls and thank you! Write a simple class and use it in a vector. Begin by writing a Student class. The public section has the following methods: Student::Student() Constructor. Initializes all data elements: name to empty string(s), numeric variables to 0. bool Student::ReadData(istream& in) Data input. The istream should already be open. Reads the following data, in this order:...
how one way of central tendency is not always the “best” way to get the one...
how one way of central tendency is not always the “best” way to get the one in the middle?
how one way of central tendency is not always the “best” way to get the one...
how one way of central tendency is not always the “best” way to get the one in the middle? thank you
Use SPSS to get the appropriate output. Along with providing the SPSS output, use the 5...
Use SPSS to get the appropriate output. Along with providing the SPSS output, use the 5 steps of hypothesis testing to analyze the results by using the p-value approach. 4. Workers went through a training program to help them in the packaging of items needed for the purchaser to assemble a toy. The number of defective packages returned per month is recorded for each employee doing the packaging. Below are the before and after number of defects per month per...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT