Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
24 views9 pages

Lab 5 - Arrays - Solutions

The document provides C programming instructions for creating, filling, displaying, inserting, and deleting elements in one-dimensional and multi-dimensional arrays. It includes code examples for various exercises, demonstrating array manipulation techniques such as sorting and matrix multiplication. The content is structured for educational purposes, likely aimed at students in a higher education setting in Tunisia.

Uploaded by

raslen gharssa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views9 pages

Lab 5 - Arrays - Solutions

The document provides C programming instructions for creating, filling, displaying, inserting, and deleting elements in one-dimensional and multi-dimensional arrays. It includes code examples for various exercises, demonstrating array manipulation techniques such as sorting and matrix multiplication. The content is structured for educational purposes, likely aimed at students in a higher education setting in Tunisia.

Uploaded by

raslen gharssa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

République Tunisienne ‫اﻟﺟﻣﮭورﯾـﺔ اﻟﺗوﻧﺳﯾـﺔ‬

Ministère de L’Enseignement ‫وزارة اﻟﺗﻌﻠﯾم اﻟﻌﺎﻟﻲ واﻟﺑﺣث‬


Supérieur de La Recherche ‫اﻟﻌﻠﻣﻲ‬
Scientifique
Université De Tunis ‫ﺟﺎﻣﻌﺔ ﺗوﻧس‬
Institut Supérieur des affaires de Tunis ‫اﳌﻌﻬﺪ اﻟﻌﺎﱄ ﻟ ٔﻋﲈل ﺑﺘﻮﺲ‬

Lab 5 – Arrays: Solutions

General C instructions to create an array, fill it, display it, insert an element in it and delete another
from a giving position :

#include<stdio.h>
#define PhysicalSize 100
main()
{
int T[PhysicalSize
PhysicalSize],i, pos,N,val;
do{
printf("enter the logical size of the array: ");
scanf("%d",&N);
}while(N<1 || N>PhysicalSize);
N>

//Fill the array


for (i=0;i<N;i++)
{
printf("Enter element T[%d] : ", i);
scanf("%d",&T[i]);
}
//Display the array
for (i=0;i<N;i++)
{
printf("%d\t",T[i]);
t",T[i]);
}

//Insert an element in the array


printf("\nEnter
nEnter value to insert\n");
insert
scanf("%d",&val);

do{
printf("Enter insertion position\n");
position
scanf("%d",&pos);
}while(pos<0||pos>N);

for (i=N;i>pos;i--)
(i=N;i>pos;i
{
T[i]=T[i-1];
}
T[pos]=val;
N++;

//Display the array to check insertion


for (i=0;i<N;i++)
<N;i++)
{
printf("%d\t",T[i]);
t",T[i]);

1
République Tunisienne ‫اﻟﺟﻣﮭورﯾـﺔ اﻟﺗوﻧﺳﯾـﺔ‬
Ministère de L’Enseignement ‫وزارة اﻟﺗﻌﻠﯾم اﻟﻌﺎﻟﻲ واﻟﺑﺣث‬
Supérieur de La Recherche ‫اﻟﻌﻠﻣﻲ‬
Scientifique
Université De Tunis ‫ﺟﺎﻣﻌﺔ ﺗوﻧس‬
Institut Supérieur des affaires de Tunis ‫اﳌﻌﻬﺪ اﻟﻌﺎﱄ ﻟ ٔﻋﲈل ﺑﺘﻮﺲ‬

do{
printf("Enter deletion position\n");
position
scanf("%d",&pos);
}while(pos<0||pos>=N);

for (i=pos;i<N-1;i++)
1;i++)
{
T[i]=T[i+1];
}
N--;

//Display the array to check deletion


for (i=0;i<N;i++)
{
printf("%d\t",T[i]);
t",T[i]);
}
}

Part I: One-dimensional array

Exercise 1:

#include<stdio.h>
#define PhysicalSize 100
main()
{
int T[PhysicalSize], N, i,j,aux;

//Enter and control the logical size


do{
printf("Please enter the size of the array\n");
array
scanf("%d",&N);
}while(N<1 || N>PhysicalSize
PhysicalSize);

// Fill the array


for(i=0;i<N;i++)
{
printf("T[%d]: ", i);
scanf("%d",&T[i]);
}

// Display the unsorted array


for(i=0;i<N;i++)
printf("%d\t",T[i]);
t",T[i]);

2
République Tunisienne ‫اﻟﺟﻣﮭورﯾـﺔ اﻟﺗوﻧﺳﯾـﺔ‬
Ministère de L’Enseignement ‫وزارة اﻟﺗﻌﻠﯾم اﻟﻌﺎﻟﻲ واﻟﺑﺣث‬
Supérieur de La Recherche ‫اﻟﻌﻠﻣﻲ‬
Scientifique
Université De Tunis ‫ﺟﺎﻣﻌﺔ ﺗوﻧس‬
Institut Supérieur des affaires de Tunis ‫اﳌﻌﻬﺪ اﻟﻌﺎﱄ ﻟ ٔﻋﲈل ﺑﺘﻮﺲ‬

// Sort the array


for(i=0;i<N-1;i++)
for(j=i+1;j<N;j++)
if (T[i]>T[j])
{
aux=T[i];
T[i]=T[j];
T[j]=aux;
}

// Display the sorted array


printf("\n");
for(i=0;i<N;i++)
printf("%d\t",T[i]);
t",T[i]);

Exercise 2:

#include<stdio.h>
#define PhysicalSize 100
main()
{
int T[PhysicalSize], N, i,j,pos,val;

//Enter and control the logical size


do{
printf("Please enter the size of the array\n");
array
scanf("%d",&N);
}while(N<1 || N>PhysicalSize
PhysicalSize);

// Fill the array


for(i=0;i<N;i++)
{
printf("T[%d]: ", i);
scanf("%d",&T[i]);
}

// Display the array before insertion


for(i=0;i<N;i++)
printf("%d\t",T[i]);
t",T[i]);

//Enter value, position and control the entered position


printf("Please enter the value to insert\n");
insert
scanf("%d",&val);
do{
printf("Please enter the position to insert the value in
in\n");

3
République Tunisienne ‫اﻟﺟﻣﮭورﯾـﺔ اﻟﺗوﻧﺳﯾـﺔ‬
Ministère de L’Enseignement ‫وزارة اﻟﺗﻌﻠﯾم اﻟﻌﺎﻟﻲ واﻟﺑﺣث‬
Supérieur de La Recherche ‫اﻟﻌﻠﻣﻲ‬
Scientifique
Université De Tunis ‫ﺟﺎﻣﻌﺔ ﺗوﻧس‬
Institut Supérieur des affaires de Tunis ‫اﳌﻌﻬﺪ اﻟﻌﺎﱄ ﻟ ٔﻋﲈل ﺑﺘﻮﺲ‬

scanf("%d",&pos);
}while(N<1 || pos>N);

// Insertion
for(i=N;i>pos;i--)
T[i]=T[i-1];
T[pos] =val;
//We don't update the logical size so that the last element gets
dropped off

// Display the array after insertion


printf("\n");
for(i=0;i<N;i++)
printf("%d\t",T[i]);
t",T[i]);

Exercise3:

#include<stdio.h>
#define PhysicalSize 100
main()
{
int T[PhysicalSize], N, i,j,val;

//Enter and control the logical size


do{
printf("Please enter the size of the array\n");
array
scanf("%d",&N);
}while(N<1 || N>PhysicalSize
PhysicalSize);

// Fill the array


for(i=0;i<N;i++)
{
printf("T[%d]: ", i);
scanf("%d",&T[i]);
}

// Display the array before deletion


for(i=0;i<N;i++)
printf("%d\t",T[i]);
t",T[i]);

//Enter value, position and control the entered position


printf("\n");
printf("Please enter the value to look for\n");
for
scanf("%d",&val);

4
République Tunisienne ‫اﻟﺟﻣﮭورﯾـﺔ اﻟﺗوﻧﺳﯾـﺔ‬
Ministère de L’Enseignement ‫وزارة اﻟﺗﻌﻠﯾم اﻟﻌﺎﻟﻲ واﻟﺑﺣث‬
Supérieur de La Recherche ‫اﻟﻌﻠﻣﻲ‬
Scientifique
Université De Tunis ‫ﺟﺎﻣﻌﺔ ﺗوﻧس‬
Institut Supérieur des affaires de Tunis ‫اﳌﻌﻬﺪ اﻟﻌﺎﱄ ﻟ ٔﻋﲈل ﺑﺘﻮﺲ‬

// Insertion
for(i=0;i<N;i++)
if(T[i]==val)
{
for (j=i;j<N;j++)
T[j]=T[j+1];
T[j-1]=0;
1]=0; //or T[N-1]=0;
T[N
break; //quiting
quiting after replacing the first occurrence of val
}

// Display the array after deletion


printf("\n");
for(i=0;i<N;i++)
printf("%d\t",T[i]);
t",T[i]);

Part II: Multi-dimensionalarray

// General program to declare a matrix,


matrix fill it and display it.

#include<stdio.h>
#define PhysicalSize 100
main()
{
int M[PhysicalSize][PhysicalSize], N1,N2, i,j,val;

//Enter and control number of rows


do{
printf("Please enter the number of rows\n");
rows
scanf("%d",&N1);
}while(N1<1 || N1>PhysicalSize);

//Enter and control number of cols


do{
printf("Please enter the number of columns\n");
columns
scanf("%d",&N2);
}while(N2<1 || N2>PhysicalSize);

// Fill the matrix


for(i=0;i<N1;i++)
for(j=0;j<N2;j++)
{
printf("M[%d][%d]: ", i,j);
scanf("%d",&M[i][j]);
}

5
République Tunisienne ‫اﻟﺟﻣﮭورﯾـﺔ اﻟﺗوﻧﺳﯾـﺔ‬
Ministère de L’Enseignement ‫وزارة اﻟﺗﻌﻠﯾم اﻟﻌﺎﻟﻲ واﻟﺑﺣث‬
Supérieur de La Recherche ‫اﻟﻌﻠﻣﻲ‬
Scientifique
Université De Tunis ‫ﺟﺎﻣﻌﺔ ﺗوﻧس‬
Institut Supérieur des affaires de Tunis ‫اﳌﻌﻬﺪ اﻟﻌﺎﱄ ﻟ ٔﻋﲈل ﺑﺘﻮﺲ‬

// Display the matrix


for(i=0;i<N1;i++)
{
for(j=0;j<N2;j++)
printf("%d
printf("%d\t",M[i][j]);
printf("\n");
}

Exercise 1:

#include<stdio.h>
#define PhysicalSize 100
main()
{
int
M1[PhysicalSize][PhysicalSize],M2[PhysicalSize][PhysicalSize],M3[Phy
sicalSize][PhysicalSize],N1,N2,N3,i,j,k,val,s;

//M1 is N1 x N2 matrix and M2 is N2 x N3 matrix.


//To compute the product of M1 and M2, nb of columns of M1 ...
// must be equal to number of row of M2.

//Enter and control number of rows of M1


do{
printf("Please enter the number of rows of M1\n");
M1
scanf("%d",&N1);
}while(N1<1 || N1>PhysicalSize);

//Enter and control number of rows of M1 and columns of M2


do{
printf("Please enter the number of columns of M1 (and rows of
M2)\n");
scanf("%d",&N2);
}while(N2<1 || N2>PhysicalSize);

//Enter and control number of rows of M1 and columns of M2


do{
printf("Please enter the number of columns of M2\n");
M2 n");
scanf("%d",&N3);
}while(N3<1 || N3>PhysicalSize);

// Fill M1
for(i=0;i<N1;i++)

6
République Tunisienne ‫اﻟﺟﻣﮭورﯾـﺔ اﻟﺗوﻧﺳﯾـﺔ‬
Ministère de L’Enseignement ‫وزارة اﻟﺗﻌﻠﯾم اﻟﻌﺎﻟﻲ واﻟﺑﺣث‬
Supérieur de La Recherche ‫اﻟﻌﻠﻣﻲ‬
Scientifique
Université De Tunis ‫ﺟﺎﻣﻌﺔ ﺗوﻧس‬
Institut Supérieur des affaires de Tunis ‫اﳌﻌﻬﺪ اﻟﻌﺎﱄ ﻟ ٔﻋﲈل ﺑﺘﻮﺲ‬

for(j=0;j<N2;j++)
{
printf("M1[%d][%d]: ", i,j);
scanf("%d",&M1[i][j]);
}
// Fill M2
for(i=0;i<N2;i++)
for(j=0;j<N3;j++)
{
printf("M2[%d][%d]: ", i,j);
scanf("%d",&M2[i][j]);
}

// Display M1
for(i=0;i<N1;i++)
{
for(j=0;j<N2;j++)
printf("%d
printf("%d\t",M1[i][j]);
printf("\n");
}
printf("\n");
// Display M2
for(i=0;i<N2;i++)
{
for(j=0;j<N3;j++)
printf("%d
printf("%d\t",M2[i][j]);
printf("\n");
}
printf("\n");
// Compute M3
for(i=0;i<N1;i++)
for(j=0;j<N3;j++)
{s=0;
for(k=0;k<N2;k++)
s = s + M1[i][k]*M2[k][j];
M3[i][j] =s;
}
// Display M3
for(i=0;i<N1;i++)
{
for(j=0;j<N3;j++)
printf("%d
printf("%d\t",M3[i][j]);
printf("\n");
}
}

Exercise 2:

7
République Tunisienne ‫اﻟﺟﻣﮭورﯾـﺔ اﻟﺗوﻧﺳﯾـﺔ‬
Ministère de L’Enseignement ‫وزارة اﻟﺗﻌﻠﯾم اﻟﻌﺎﻟﻲ واﻟﺑﺣث‬
Supérieur de La Recherche ‫اﻟﻌﻠﻣﻲ‬
Scientifique
Université De Tunis ‫ﺟﺎﻣﻌﺔ ﺗوﻧس‬
Institut Supérieur des affaires de Tunis ‫اﳌﻌﻬﺪ اﻟﻌﺎﱄ ﻟ ٔﻋﲈل ﺑﺘﻮﺲ‬

#include<stdio.h>
#define PhysicalSize 100
main()
{
int
M1[PhysicalSize][PhysicalSize],M2[PhysicalSize][PhysicalSize],M3[Phy
sicalSize][PhysicalSize],N1,N2,N3,i,j,k,val,s;

//Enter and control number of rows of M1


do{
printf("Please enter the number of rows of M1\n");
M1
scanf("%d",&N1);
}while(N1<1 || N1>PhysicalSize);

//Enter and control number of rows of M1 and columns of M2


do{
printf("Please enter the number of columns of M1\n");
M1 n");
scanf("%d",&N2);
}while(N2<1 || N2>PhysicalSize);

// Fill M1
for(i=0;i<N1;i++)
for(j=0;j<N2;j++)
{
printf("M1[%d][%d]: ", i,j);
scanf("%d",&M1[i][j]);
}

// Display M1
for(i=0;i<N1;i++)
{
for(j=0;j<N2;j++)
printf("%d
printf("%d\t",M1[i][j]);
printf("\n");
}

printf("\n");

// Compute Transpose M2
for(i=0;i<N1;i++)
for(j=0;j<N2;j++)
M2[j][i]= M1[i][j];

// Display M2
for(i=0;i<N2;i++)
{
for(j=0;j<N1;j++)
printf("%d
printf("%d\t",M2[i][j]);

8
République Tunisienne ‫اﻟﺟﻣﮭورﯾـﺔ اﻟﺗوﻧﺳﯾـﺔ‬
Ministère de L’Enseignement ‫وزارة اﻟﺗﻌﻠﯾم اﻟﻌﺎﻟﻲ واﻟﺑﺣث‬
Supérieur de La Recherche ‫اﻟﻌﻠﻣﻲ‬
Scientifique
Université De Tunis ‫ﺟﺎﻣﻌﺔ ﺗوﻧس‬
Institut Supérieur des affaires de Tunis ‫اﳌﻌﻬﺪ اﻟﻌﺎﱄ ﻟ ٔﻋﲈل ﺑﺘﻮﺲ‬

printf("\n");
}
// 2nd way : Display transpose of M1 without using M2
printf("\n");
for(j=0;j<N2;j++)
{
for(i=0;i<N1;i++)
printf("%d
printf("%d\t",M1[i][j]);
printf("\n");
}

// Display bottom half of M1


printf("\n");
for(i=0;i<N1;i++)
{
for(j=0;j<=i;j++)
printf("%d
printf("%d\t",M1[i][j]);
printf("\n");
}

You might also like