Part 1: Writing the code Create a \'cs212\' folder in your home folder. Create a
ID: 638934 • Letter: P
Question
Part 1: Writing the code
Create a 'cs212' folder in your home folder. Create a 'lab1' folder inside the 'CS212' folder.
Create a file in the text editor named "lab1.c", and save it to the 'cs211/lab1' folder you just created.Open Terminal. Using the 'cd ~/cs211/lab1' command (the '~' is a stand-in for your home directory), change the directory to your lab directory you just created. Once you are in the 'lab1' folder, you can edit the text file you created in 2 different ways:
Using command 'nano lab1.c'. (nano tutorial)
Open the file in the GUI text editor (gEdit).
Write a short program that generates random numbers between 0 and 1000000 inside a loop. The number of times you loop will also be determined by a random number between 1 and 10.
You should generate a random number between 1 and 10 to determine how many times you will loop.
Within your loop, generate a random number between 0 and 1000000 and print to the console using printf().
Save and exit.
Part 2: Compiling and Executing
Like you did in the previous step, navigate to your lab folder in the shell.
Type in 'ls' to list the directory's files. Ensure that 'lab1.c' is in the directory.
To compile, we will be using a 'makefile'.
Example Temple For makefile
all: lab1
lab1: lab1.o
gcc -Wall lab1.o -lm -o lab1
lab1.o:
gcc -Wall -c lab1.c -o lab1.o
clean:
rm -rvf *.o lab1
submit: .
rm -rvf *.o lab1
tar -czvf lab1.tar.gz .
The source should compile to a working executable. You can run the executable with the command, './. In this case, './lab1'.
Part 3
Using a makefile, create a tar archive (command: make submit).
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a;
int d;
int i;
a=rand()%10+1; //number of times the looping will be done
for(i=0;i<a;i++)
{
d=rand()%1000000;
printf(" random number generated is %d ",d);
}
}