Vous êtes sur la page 1sur 9

PART B : USING FUNCTIONS

1. Modify the previous program in PART A by adding another function prototype name
loop type void after step 7/
2. Modify the main function as below:

main ( )
{
loop ( ); //a function call
}

3. Type the following statements in the loop function definition:

int x;
for (x=1; x<=10; x++)
{ printf(“%d”, square (x));
}
Printf(“\n”);

4. Execute the program and observe the output.


PART F : SORTING ARRAY

1. Create a new project named SortArray.


2. Include the stdio.h header file.
3. Define a constant named max with vale of 8.
4. Define the main function.
5. Declare three integer variables named i, large, and small.
6. Type the following statements:

int num[max] = {4, 8, 13, 0, -5, 3, 20, -1};

printf(“Array: “);
for (i=0; i<=max; i++)
printf(“%d”, num[i]);

large=num[0];
small=num[0];

for(i=0; i<max; i++) {


if(num[i] > large)
large=num[i];

if(num[i] < small)


small=num[i];
}

7. Using printf statement, print out the large and small values.
8. Execute the program and observe the output.
PART G : PASSING ARRAY TO FUNCTION

1. Create a new project named MultiArray.


2. Include the stdio.h header file.
3. Declare a prototype integer function called addNumbers with one parameter, an integer
array.
4. Define the main function.
5. Inside the main function, declare an integer variable named i.
6. Then, declare an integer array variable named array with 5 number of element.
7. Type the following statements inside the main function:

printf(“Enter 5 integers separated by spaces: “);


for(i=0; i<5; i++) {
scanf(“%d”, &array[i]);
}
printf(“\nTheir sum is: %d\n”, addNumbers(array));

return 0;

8. Define addNumbers function with an integer parameter named fiveNumbers.


9. Inside the addNumbers() function, declare an integer an integer variable named i.
10. Then, declare an integer variable named sum and assign value of 0 to the variable.
11. Type the following statements inside the addNumbers function.

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


sum+=fiveNumbers[i];
return sum;

12. Execute the program and observe the output.


PART H : MULTI-DIMENSIONAL ARRAY

1. Create a new project named MultiArray.


2. Include the stdio.h header file and define the main function.
3. Declare two integer variables named i and j.
4. Declare an integer array variable named arr with 2-dimensional index, row of 2 and
column of 6.
5. Type the following statements:

for(i=0; i<2; i++) {


printf(“Enter 6 integers separated by spaces: “);
for(j=0; j<6; j++) {
scanf(“%d”, &arr[i][j]);
}
printf(“\n”);
}

printf(“You entered: \n”);


for(i=0; i<2; i++) {
for(j=0; j<6; j++) {
printf(“%d”, arr[i][j]);
}
printf(“\n”);
}

6. Execute the program and observe the output.


CONCLUSION

From this experiment 4, we have learned a lot about the Function in C. A function has three

categories. First, Functions with no arguments (parameter) and no return values. Second,
Functions with arguments (parameters) but no return values and third are Functions with
argument and return values. And has another one that called Recursion Function. Recursion
Function means the function that calls itself either directly or indirectly through another function.
Overall, function means a mini-program where a group of statements are executed. When a user
or the programmer defined function, it will break a large into smaller sets of statements
according to their task. We also had discover that by using function, it have many benefit such as
the program that we make be more manageable. Next, the software is reusability and it will avoid
repeating code so that we don’t need to make the coding more times.
CONCLUSION

From this experiment 5, we had learned a lot about the arrays. An array must have bracket

symbol ‘[ ]‘. An array is a group of memory locations that have the same name and the same
types. It use to allows to store a sequence of values or in the other words, it like a hard disk, the
memory that can save so many values. For instance, if we type int a[5]; it means it store 5
values for example int a[5] = {10,20,30,40,50}; . Array also can store characters too.
Character arrays are capable of storing strings. This is the special ability of an array. To use it we
should type like

char string [ ] = “Hello”

But, the function scanf will read characters until computer detect a space, tab, newline or EOF
(end of function)

Instead of one-dimensional array, it also has two-dimensional array or multi-dimensional array.


It used to represent table of values consisting of information arranged in rows and column. For
example:

int array [2] [4];

It means that it has 2 rows and 4 columns. It is similar with matrix equation.
TWO-DIMENSIONAL ARRAY

A two-dimensional array is an array in with both rows and columns. In contrast, gave one
dimensional array is simply a linear sequence of elements.

When we define or use a two-dimensional array, we must supply both the row and column
numbers. Two-dimensional arrays are good for modeling flat surfaces, for example, the screen of
a computer, the layout of a warehouse, or a map. Capital flat surface has two dimensions, and so
a two-dimensional array can easily model it.

Defining a two-dimensional array is very similar to defining a one-dimensional array. You


simply need to use an extra pair of brackets for the additional dimension. Here's an example.

int array [2][6];

This tell the computer that it have 2 row and 6 column. It will be like:

int array {{1,2,3,4,5,6},{6,5,4,3,2,1}};

And the output will be like:

1 2 3 4 5 6
6 5 4 3 2 1
RETURN VALUE DATA TYPE OF FUNCTION

If a function is to use arguments, it must declare variables that accept the values of the
arguments. These variables are called the formal parameters of the function. The formal
parameters behave like other local variables inside the function and are created upon entry into
the function and destroyed upon exit.

While calling a function, there are two ways that arguments can be passed to a function:

Call by value :

This method copies the actual value of an argument into the formal parameter of the function. In
this case, changes made to the parameter inside the function have no effect on the argument

Call by reference :

This method copies the address of an argument into the formal parameter. Inside the function,
the address is used to access the actual argument used in the call. This means that changes made
to the parameter affect the argument.

In general, this means that code within a function cannot alter the arguments used to call the
function and above mentioned example while calling max() function used the same method.
FUNCTION WITH ARGUMENTS AND RETURN VALUES

Function with arguments and return value has same meaning with other function with arguments
that means it will sent arguments from the calling function to the called function but it expects
the result to be returned back from the called function back to the calling function.

Vous aimerez peut-être aussi