3
United International University Department of Computer Science and Engineering CSI 124: Advanced Programming Laboratory Lab Work # 1 Summer 2015 1 Task 1: Using the random numbers In this task, you will be able to declare and fill up a two dimensional array of integers with random numbers. For that you need to generate random numbers using the function rand() from stdlib.h. Do you know the prototype? int rand (void); Returns a pseudo-random integral number in the range between 0 and RAND MAX (library dependent, generally 32767). Do we remember how to declare two dimensional arrays? Lets try this following code: #include<stdlib.h> #include<stdio.h> int main() { int myArray[8][6]; // this is the two dimensional array int i,j; for(i=0;i<8;i++) { for(j=0;j<6;j++) { myArray[i][j]=rand(); } } // now lets print this array printf("The array:\n"); for(i=0;i<8;i++) { for(j=0;j<6;j++) { printf("%d ",myArray[i][j]); } printf("\n"); } return 0; } 1. Now try to compile this code in the IDE and see the output.

lab_01

Embed Size (px)

DESCRIPTION

c++lab fileuiu

Citation preview

Page 1: lab_01

United International UniversityDepartment of Computer Science and Engineering

CSI 124: Advanced Programming LaboratoryLab Work # 1Summer 2015

1 Task 1: Using the random numbers

In this task, you will be able to declare and fill up a two dimensional array of integers with random numbers.For that you need to generate random numbers using the function rand() from stdlib.h. Do you know theprototype?

int rand (void);

Returns a pseudo-random integral number in the range between 0 and RAND MAX (library dependent,generally 32767). Do we remember how to declare two dimensional arrays? Lets try this following code:

#include<stdlib.h>

#include<stdio.h>

int main()

{

int myArray[8][6]; // this is the two dimensional array

int i,j;

for(i=0;i<8;i++)

{

for(j=0;j<6;j++)

{

myArray[i][j]=rand();

}

}

// now lets print this array

printf("The array:\n");

for(i=0;i<8;i++)

{

for(j=0;j<6;j++)

{

printf("%d ",myArray[i][j]);

}

printf("\n");

}

return 0;

}

1. Now try to compile this code in the IDE and see the output.

Page 2: lab_01

2. Use a modulus operator along with the rand() function. Fill the two dimensional array with valuesfrom 1-8 only.

Note that the same thing could have been done in two other ways: one by initializing the array andanother by taking the elements of the array as inputs from the user using keyboard (remember scanf()function in nested loops).

2 Task 2: Remember Functions?

In this task, you will be able to write and use a function. We want you to create a function to print or showthe values of the array. Previously it was done within the main function. Can you write a function that willtake a two dimensional array as parameter and print it in the console? Try it yourself first and then see thefollowing code.

#include<stdlib.h>

#include<stdio.h>

void print2DArray(int myArray[8][6]); // this is prototype - why?

int main()

{

int myArray[8][6]; // this is the two dimensional array

int i,j;

for(i=0;i<8;i++)

{

for(j=0;j<6;j++)

{

myArray[i][j]=rand();

}

}

// now lets print this array with help of the function

print2DArray(myArray);

return 0;

}

// here is the definition of the function

void print2DArray(int array[8][6])

{

int i,j;

printf("The array:\n");

for(i=0;i<8;i++)

{

for(j=0;j<6;j++)

{

printf("%d ",array[i][j]);

}

printf("\n");

}

}

Note, we did the same thing, and the output is same except that this time we used a function and passedour 2D array to this function. Since that function knows the dimensions (how? parameter) it was able toprint the array. Now do the following:

2

Page 3: lab_01

2.1 Exercise:

Now write another function with the following prototype:int rowSum(int array[8][6],int rowNumber);

This function will return the sum of the row given as parameter rowNumber.

2.2 Exercise:

Now write another function that will find the row number with maximum sum. The prototype is not given.

3 Task 3: Survivors

Now that all the cells in the array has integers from 1-8 and a cell has maximum 8 neighbors (what is aneighbor?). Now a cell in the next generation will have a value equal to the value that is maximally presentin the neighboring cells. Given a cell by row number and cell number you have to find what will be the valuein that cell in the next generation. A cell survives if it retains the same value in the next generation.

3.1 Exercise:

Now write a function that will take the two dimensional array and row and column number of a cell and findthe values that it will have in the next generation.

3.2 Exercise:

Now write a function that will take the two dimensional array and determine whether there is any survivorin the next generation or not. Your function will print, “No survivors!” if there is not survivor and will print,“Survivor exists!” if at least one of the cells survive.

4 Marks Distribution

Task 1: 25%, Task 2: 50%, Task 3: 25%

5 Practice At Home

Can you try to find the value of the cells in the next generation? If you can do that, then try to run theprogram for a number of iterations. See what happens to the values in this array. Any thing strange? (bonusmarks if you can show this in the next class!)

6 More Practice

1. Try to know about the function srand().

2. Write functions to find diagonal sums.

3. Try to find the maximum element in the array and its position (row and column number).

4. Write a program/function to search an element whether it is present in a two dimensional array or not.If present then also find the position.

5. Find the second largest element and its position in an array.

6. Find the element with highest frequency in the array.

3