Docsity

C Programming Exercises: Array Manipulation, Exercises of Computer science

Toggle sidebar
bg1
Correction Lab4 Arrays
Exercise V.I.1.
#include <stdio.h>
int main()
{
int size = 5;
int tab[size];
int i,j;
for( i = 0; i < size; i++){
printf("enter the value %d: ",i);
scanf("%d",&tab[i]);
}
int extra;
printf("enter the extra value");
scanf("%d",&extra);
int posExtra;
do{
printf("enter the position of the extra value");
scanf("%d",&posExtra);
pf3
pf4
pf5

Partial preview of the text

Download C Programming Exercises: Array Manipulation and more Exercises Computer science in PDF only on Docsity!

Exercise V.I.1.

#include <stdio.h> int main() { int size = 5; int tab[size]; int i,j; for( i = 0; i < size; i++){ printf("enter the value %d: ",i); scanf("%d",&tab[i]); } int extra; printf("enter the extra value"); scanf("%d",&extra); int posExtra; do{ printf("enter the position of the extra value"); scanf("%d",&posExtra);

}while((posExtra < 0) || (posExtra > size)); for(i = size-1; i > posExtra; i--){ tab[i] = tab[i-1]; } tab[posExtra] = extra; for( i = 0; i < size; i++){ printf("%d",tab[i]); }

if(tab[i]==extra){ position = i; found = true; }else{ i++; } } for(i = position; i<size-1; i++ ){ tab[i]= tab[i+1]; } tab[size-1] = 0; for( i = 0; i < size; i++){ printf("%d",tab[i]); }

Exercise V.I.3.

#include <stdio.h> main() { int size = 6; int tab[size]; int i,j; for( i = 0; i < size; i++){ printf("enter the value %d: ",i); scanf("%d",&tab[i]); } int permutation; for( i = 0; i < size; i++){ for( j = i+1; j < size; j++){ if(tab[i]>tab[j]){ permutation = tab[j]; tab[j] = tab[i]; tab[i] = permutation; }