0% found this document useful (0 votes)
58 views

ES085 Sample Programs With Array

The document contains 5 problems involving arrays in C programming. Problem 1 creates a 1D array of 10 numbers, prints the 3rd and 8th elements, and prints all elements. Problem 2 creates a 1D array, inputs numbers, and finds the highest number. Problem 3 creates an array, swaps the 3rd and 8th elements, and reprints the array. Problem 4 creates an array, calculates the sum of all elements, and prints the sum. Problem 5 creates a 2D matrix with user-defined rows and columns, inputs elements, and prints the matrix. Each problem demonstrates different array operations in C.

Uploaded by

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

ES085 Sample Programs With Array

The document contains 5 problems involving arrays in C programming. Problem 1 creates a 1D array of 10 numbers, prints the 3rd and 8th elements, and prints all elements. Problem 2 creates a 1D array, inputs numbers, and finds the highest number. Problem 3 creates an array, swaps the 3rd and 8th elements, and reprints the array. Problem 4 creates an array, calculates the sum of all elements, and prints the sum. Problem 5 creates a 2D matrix with user-defined rows and columns, inputs elements, and prints the matrix. Each problem demonstrates different array operations in C.

Uploaded by

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

PROBLEM 1

//Create an array of 10 numbers

//output the contents of the 3rd and 8th locations

//output all the contents of the array

#include<stdio.h>

#include<conio.h>

int main()

int num[10]={0}; //initialize te contents of the array to 0

int ctr=0;

//creation of the array

for(ctr=0; ctr<10; ctr++)

printf("Enter element of num[%d]:", ctr);

scanf("%d", &num[ctr]); //kindly observe how array is done in scanf

//print the contents of location 3 and 4

printf("*********Printing the contents of location 3 and 8\n");

printf("num[3]=%d",num[3]); //printing the content of location 3

printf("\nnum[8]=%d", num[8]); //printing the content of location 8

//printing the entire contents of the array; loop is to be used to access each element

printf("\n*********Printing all the contents of the array\n");

for(ctr=0; ctr<10; ctr++)

printf("num[%d]=%d", ctr, num[ctr]);


printf("\n");

return 0;

PROBLEM 2

//Create an array of 10 numbers

//output the highest number in the array

#include<stdio.h>

#include<conio.h>

int main()

int num[10]={0}; //initialize te contents of the array to 0

int ctr=0, high;

//creation of the array

for(ctr=0; ctr<10; ctr++)

printf("Enter element of num[%d]:", ctr);

scanf("%d", &num[ctr]); //kindly observe how array is done in scanf

//Searching the highest number in the array

high=num[0]; //Assume the the first element is the highest

for(ctr=0; ctr<10; ctr++)

{
if(num[ctr+1]>high) //start comparing element ctr+1m don't compare to your self

high=num[ctr+1];

printf("\n*********Printing highest number in the array\n");

printf("Highest is %d", high);

return 0;

PROBLEM 3

//Create an array of 10 numbers

//Print the contents of the original array

//Swap the contents of location 3 and location 8

//Print the new contents of the array

#include<stdio.h>

#include<conio.h>

int main()

int num[10]={0}; //initialize te contents of the array to 0

int ctr=0, temp;

//creation of the array

for(ctr=0; ctr<10; ctr++)

printf("Enter element of num[%d]:", ctr);


scanf("%d", &num[ctr]); //kindly observe how array is done in scanf

printf("*********Printing contents of the original array");

for(ctr=0; ctr<10; ctr++)

printf("\nnum[%d]=%d", ctr, num[ctr]);

//This is how to swap the contents of location 3 and location 8

temp=num[3]; //assign the content of location 3 to temp

num[3]=num[8]; //assign the content of location to location 3

num[8]=temp; //assign the content of temp to location to location 8

printf("*********Printing contents of the new array\n");

for(ctr=0; ctr<10; ctr++)

printf("\nnum[%d]=%d", ctr, num[ctr]);

return 0;

PROBLEM 4

//Create an array of 10 numbers

//Print the sum of the contents of the array

#include<stdio.h>

#include<conio.h>
int main()

int num[10]={0}; //initialize te contents of the array to 0

int ctr=0, sum=0;

//creation of the array

for(ctr=0; ctr<10; ctr++)

printf("Enter element of num[%d]:", ctr);

scanf("%d", &num[ctr]); //kindly observe how array is done in scanf

printf("*********Printing contents of the original array");

for(ctr=0; ctr<10; ctr++)

printf("\nnum[%d]=%d", ctr, num[ctr]);

//This is how to get the sum of the contents of the array

for(ctr=0; ctr<10; ctr++)

sum+=num[ctr];

printf("\n********The sum=%d", sum);

return 0;

}
PROBLEM 5

//Create a multidimensional array

//Print the contents of the multidimensional array

//Create an nxm Matrix, n=row, m=column

#include<stdio.h>

#include<conio.h>

int main()

int Matrix[100][100]; //maximize the row a and columns of Matrix to 100

int row, col, maxrow, maxcol;

printf("How many rows and columns is your Matrix?:");

scanf("%d %d", &maxrow, &maxcol);

//creation of the Matrix

for(row=0; row<maxrow; row++)

for(col=0; col<maxcol; col++)

printf("Enter element of Matrix[%d %d]:", row, col);

scanf("%d", &Matrix[row][col]);

} //kindly observe how multidimensional array is done in scanf

printf("*********Printing contents of the Matrix\n");

for(row=0; row<maxrow; row++)

for(col=0; col<maxcol; col++)


{

printf("%d ", Matrix[row][col]);

printf("\n"); // to print the elements of next row

return 0;

You might also like