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

Array Programs

The document contains several C programming examples that demonstrate the use of arrays: 1. A simple program that defines an integer array of size 10 and prints out each element. 2. A program that counts the number of positive and negative numbers in an integer array. 3. A program that adds two matrices of the same order and stores the results in a third matrix.

Uploaded by

Bijay Ojha
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
145 views

Array Programs

The document contains several C programming examples that demonstrate the use of arrays: 1. A simple program that defines an integer array of size 10 and prints out each element. 2. A program that counts the number of positive and negative numbers in an integer array. 3. A program that adds two matrices of the same order and stores the results in a third matrix.

Uploaded by

Bijay Ojha
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Array programs

1. A simple program to demonstrate the definition and use of an array. #include<stdio.h> main() { int i[10],j; for ( j=0 ; j<10 ; j++) i[j] = j ; for ( j=0 ; j<10 ; j++) printf("%d\n" , i[j]) ; } 2. Program to count the no of positive and negative numbers #include< stdio.h > void main( ) { int a[50],n,count_neg=0,count_pos=0,I; printf(Enter the size of the array\n); scanf(%d,&n); printf(Enter the elements of the array\n); for I=0;I < n;I++) scanf(%d,&a[I]); for(I=0;I < n;I++) { if(a[I] < 0) count_neg++; else count_pos++; } printf(There are %d negative numbers in the array\n,count_neg); printf(There are %d positive numbers in the array\n,count_pos);

3. example program to add two matrices & store the results in the 3rd matrix #include< stdio.h > #include< conio.h > void main() { int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q; clrscr(); printf(enter the order of the matrix\n);

scanf(%d%d,&p,&q); if(m==p && n==q) { printf(matrix can be added\n); printf(enter the elements of the matrix a); for(i=0;i < m;i++) { for(j=0;j < n;j++) { scanf(%d,&a[i][j]); } } printf(enter the elements of the matrix b); for(i=0;i < p;i++) { for(j=0;j < q;j++) { scanf(%d,&b[i][j]); } } printf(the sum of the matrix a and b is); for(i=0;i < m;i++) { for(j=0;j < n;j++) { c[i][j]=a[i][j]+b[i][j]; } } for(i=0;i < m;i++) { for(j=0;j < n;j++) printf(%d\t,&a[i][j]); printf(\n); } } 4. Program to find a particular number from a list of number. #include< stdio.h > void main( ) { int a[50],num=0,n,count_pos=0; printf(Enter the size of the array\n); scanf(%d,&n); printf(Enter the elements of the array\n);

for I=0;I < n;I++) scanf(%d,&a[I]); printf(Enter the number that you want to find from the array\n); scanf(%d,&num); for(I=0;I < n;I++) { if(a[I] == num) count_pos=count_pos+1; } If (count_pos>0) printf(The Entered Number is present %d number of time,count_pos); else printf(The Entered Number is not present.); } /* PROGRAM FOR DELETE THE ARRAY ELEMENT FROM THE LIST */ #include<stdio.h> #include<conio.h> void main() { int arr[6],i=0,j=0,count=0,num=0,Flag=1; clrscr(); printf("\n\nProgram to DELETE the number from the Array"); printf("\n\nEnter The six number into the array"); for(count=0; count<6; count++) { printf("\n\n Enter The Number into %d Location of Array -->",count); scanf("%d",&arr[count]); } i=count; printf("\n\n Enter The Number You Want to delete from the list-->"); scanf("%d",&num); for(count=0; count<i; count++) { if(arr[count]==num) { Flag=0; arr[count]=0; for(j=count; j<i; j++) arr[j]=arr[j+1]; } } if(Flag==0) { printf("\n\n The New Element's in array is\n\n");

for(count=0; count<(i-1); count++) printf("%d\t",arr[count]); } else { printf("\n\n There is no such number in the list.."); printf("\n\n Enter Any key to terminate the program"); getch(); exit(1); } getch();

6. Program for to count the number of even and odd number present in the array #include<stdio.h> #include<conio.h> Void main() { int arr[50],size, count_even,count_odd; clrscr(); printf("\n\nEnter The size of the array less then 50"); scanf(%d,&size); for(count=0; count<size; count++) { printf("\n\n Enter The Number into %d Location of Array ",count); scanf("%d",&arr[count]); } for(count=0; count<size; count++) { if(arr[count]%2==0) { count_even++; printf(The Even Number is= %d,arr[count]); } elseif(arr[count]%2!=0) { count_odd++; printf(The Odd Number is= %d,arr[count]); } } printf(The Number of Even Number is = %d,count_even); printf(The Number of odd Number is = %d,count_odd); } 7. compute the sum of the elements of an array #include <stdio.h> #define SIZE 12

int main() { // declare and initialize the array named a with size SIZE int a[SIZE] = {1,3,5,4,7,2,99,16,45,67,89,45}; // declare two normal variables int i, total = 0; // do the loop for the array... for(i = 0; i <= (SIZE-1); i++) { // display the array and its element... printf("\n a[%d]= %d", i, a[i]); // total up the array total = total + a[i];

} printf("\nThe sum of the array elements is %d\n", total); return 0; } 8. sorting an array values into ascending order #include <stdio.h> #define SIZE 10 int main() { int a[SIZE] = {34,6,41,58,0,12,89,-2,45,25}; int i, pass, hold; printf("Data items in original order\n\n"); // displaying the original array... for(i=0; i<=SIZE - 1; i++) printf("%d ", a[i]); // ------do the sorting...ascending------------// for every array elements do this... for(pass = 1; pass <= (SIZE-1); pass++) // for every 2 array elements comparison do // the comparison and swap... for(i = 0; i <= (SIZE-2); i++) // set the condition... if(a[i] > a[i + 1]) { // put the a[i] in temporary variable hold... hold = a[i];

// put the a[i + 1] in a[i] a[i] = a[i + 1]; // put the hold in a[i + 1], one swapping is // completed...and repeat for other elements... a[i + 1] = hold; } printf("\n\nData items in ascending order\n\n"); // display the new ordered list... for (i=0; i <= (SIZE-1); i++) printf("%4d", a[i]); printf("\n\n"); return 0; } Output:

By changing the following code in the above program, recompile and re run the program. You will get the descending order. if(a[i] > a[i + 1]) to if(a[i] < a[i + 1]) The following is the output.

/* a simple sorting program that sort a list of n */ /* integer numbers, entered by the user (ascending) */ #include <stdio.h> #define maxsize 100 int main() { int temp, i, j, n, list[maxsize]; printf("\n--You are prompted to enter your list size.--"); printf("\n--Then, for your list size, you are prompted to enter--"); printf("\n--the element of your list.--"); printf("\n--Finally your list will be sorted ascending--\n"); /* get the list size... */ printf("\nEnter your list size: "); scanf(" %d", &n); /* prompting the data from user store in the list... */ for(i=0; i<n; i++) { printf("Enter list's element #%d -->", i); scanf("%d", &list[i]); } // do the sorting... for(i=0; i<n-1; i++) for(j=i+1; j<n; j++) if(list[i] > list[j]) { /* These three lines swap the elements */ /* list[i] and list[j]. */ temp = list[i]; list[i] = list[j]; list[j] = temp; } printf("\nSorted list, ascending: "); for(i=0; i<n; i++) printf(" %d", list[i]); printf("\n"); return } 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy