CMPE 108 Programming Fundamentals
Laboratory Work 7: Arrays
OBJECTIVES:
1. Understanding arrays.
2. Using single and multi dimensional arrays.

You should do the preliminary work before coming to the laboratory
session. You will not be graded on the preliminary work.

You will be asked to write a new C program (question 3) during the
laboratory session. You can actually work on it before coming to the
lab as well.
PRELIMINARY WORK (questions 1 and 2):
1. Write a complete C program to evaluate each the following C program
fragments and write the output into the given boxes on the right.
Part a-)
age[0]);
age[1]);
age[2]);
age[3]);
Page
1
int age[4];
age[0]=2;
age[1]=43;
age[2]=60;
age[3]=46;
printf("%d\n",
printf("%d\n",
printf("%d\n",
printf("%d\n",
Prepared by Dr. Yıltan Bitirim, Mohammed Ahmed Shah and Erhan Basri.
Modified by Gürcü Öz and Yıltan Bitirim (2009-2010), Zeki Bayram (2014).
Part b-)
int age[4] = {23, 34, 65, 74};
int same_age[4];
int i;
for(i=0; i<4; i++)
same_age[i]=age[i];
for(i=0; i<4; i++)
printf("same_age[%d] =
%d\n", i, same_age[j]);
Part c-)
int i;
float sales[120];
float sum=0.0;
for (i = 0; i < 10; i++)
{
printf("\nEnter Sales ($): ");
scanf("%f", &sales[i]);
}
for (i=0;i<10;i++)
sum+=sales[i];
printf(“sum of sales is %f”, sum);
Part d-)
int n, m;
float x[5][6];
for (n = 0; n < 5; ++n)
for (m = 0; m < 6; ++m)
{
printf(“Enter value for x[%d][%d]”,n,m);
scanf("%f", &x[n][m]);
Page
2
}
for (n = 0; n < 5; n++)
for (m = 0; m < 6; m++)
printf("\nx[%d][%d]=%f", n,m,x[n][m]);
Prepared by Dr. Yıltan Bitirim, Mohammed Ahmed Shah and Erhan Basri.
Modified by Gürcü Öz and Yıltan Bitirim (2009-2010), Zeki Bayram (2014).
2. Trace the following C program and find and describe the output.
#include <stdio.h>
int main(void)
{
int a[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
int b[3][3] = { {10, 11, 12}, {13, 14, 15}, {16, 17, 18} };
int c[3][3] = { {0, 0, 0}, {0, 0, 0}, {0, 0, 0} }; /*result
array initialized to zero */.
int i, j, k;
printf("\n\nMATRIX a\n");
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
printf("%d\t", a[i][j]);
printf("\n");
}
/* ‘\t’ is a tab sequence*/
/* ‘\n’ is a new line
sequence*/
/* displaying the array 'a'
in matrix fashion*/
printf("\n\nMATRIX b\n");
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
printf("%d\t", b[i][j]);
printf("\n");
}
/* displaying the
array 'b' in matrix
fashion*/
/*multiplying
matrix 'a' with
matrix 'b'*/
/*result goes in
matrix ‘c’*/
for(i=0; i<3; i++)
for(j=0; j<3; j++)
for(k=0; k<3; k++)
c[i][j] += a[i][k] * b[k][j];
printf("\n\nMATRIX c = a x b\n");
return 0;
}
Prepared by Dr. Yıltan Bitirim, Mohammed Ahmed Shah and Erhan Basri.
Modified by Gürcü Öz and Yıltan Bitirim (2009-2010), Zeki Bayram (2014).
3
// displaying the result
Page
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
printf("%d\t", c[i][j]);
printf("\n");
}
3. To be done in groups of two. Write a C program to input an integer, and
output how many times each digit occurs in the integer. Use the program given
in slides 16-19 of chapter 8 as the starting point. Example run of the program
should be like:
Page
4
enter integer: 12254933
Frequency of digits are:
0 : 0 time(s)
1: 1 time(s)
2: 2 time(s)
3: 2 time(s)
4: 1 time(s)
5: 1 time(s)
6: 0 time(s)
7: 0 time(s)
8: 0 time(s)
9: 1 time(s)
Prepared by Dr. Yıltan Bitirim, Mohammed Ahmed Shah and Erhan Basri.
Modified by Gürcü Öz and Yıltan Bitirim (2009-2010), Zeki Bayram (2014).
Download

CMPE 108 Programming Fundamentals