Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Array Codes
- 1A. Write a program to store the elements in 1-D array and perform the
- operations like searching, sorting and reversing the elements. [Menu Driven]
- #include <stdio.h>
- #include <stdlib.h>
- void bubble_sort(int b[], int n);
- void reverse(int b[], int n);
- void search(int b[], int n);
- void main(){
- int a[7] = {10, 45, 51, 20, 75, 30, 35};
- int i, ch = 1, n;
- n = 7;
- printf("\n A : ");
- for (i = 0; i < n; i++)
- printf("\t %d", a[i]);
- while (ch != 4) {
- printf("\n 1.Sort \n 2.Reverse \n 3.Searching \n 4.Exit \n Select the option : ");
- scanf("%d", &ch);
- switch (ch) {
- case 1:
- bubble_sort(a, n);
- break;
- case 2:
- reverse(a, n);
- break;
- case 3:
- search(a, n);
- break;
- case 4:
- exit(0);
- }
- }
- }
- void bubble_sort(int b[], int n) {
- int p, i, temp;
- for (p = 0; p < n - 1; p++) {
- for (i = 0; i < n - 1; i++) {
- if (b[i] > b[i + 1]) {
- temp = b[i];
- b[i] = b[i + 1];
- b[i + 1] = temp;
- }
- }
- }
- printf("\n Array : ");
- for (i = 0; i < n; i++)
- printf("\n %d", b[i]);
- printf("\n \n Sorted Array : ");
- for (i = 0; i < n; i++) {
- printf("\t %d", b[i]);
- }
- }
- void reverse(int b[], int n) {
- int i;
- printf("\n Array : ");
- for (i = 0; i < n; i++)
- printf("\t %d", b[i]);
- printf("\n \n Reversed Array : ");
- for (i = n - 1; i >= 0; i--)
- printf("\t %d", b[i]);
- }
- void search(int b[], int n) {
- int data, i, flag = 0;
- printf("\n Array : ");
- for (i = 0; i < n; i++)
- printf("\t %d", b[i]);
- printf("\nEnter element to search: ");
- scanf("%d", &data);
- for (i = 0; i < n; i++) {
- if (b[i] == data) {
- printf("\n The element is present at position %d", i + 1);
- flag = 1;
- break;
- }
- }
- if (flag == 0) {
- printf("\n The element is not present in the array!!");
- }
- }
- 1B. Read the two arrays from the user and merge them and display the
- elements in sorted order. [Menu Driven]
- #include<stdio.h>
- #include<conio.h>
- void main(){
- int n1, n2, n3, i, j; //array size declaration!
- int a[10], b[10], c[20];
- clrscr();
- printf("Enter size of the first array! : \n");
- scanf("%d",&n1);
- printf("\nEnter the elements of array : ");
- for(i=0;i<n1;i++){
- scanf("%d",&a[i]);
- }
- printf("\n Enter the size of second array : ");
- scanf("%d", &n2);
- printf("\n Enter the elements in second array! : ");
- for(i=0; i<n2; i++){
- scanf("%d", &b[i]);
- }
- n3=n1+n2;
- for(i=0; i<n1; i++){
- c[i]=a[i];
- }
- for(i=0; i<n2; i++){
- c[i]=a[i];
- }
- for(i=0; i<n2; i++){
- c[i+n1]=b[i];
- }
- printf("\n Final arrray after sorting : ");
- for(i=0; i<n3; i++){
- int temp;
- for(j=i+1; j<n3; j++){
- if(c[i]>c[j]){
- temp=c[i];
- c[i]=c[j];
- c[j]=temp;
- }
- }
- }
- for(i=0; i<n3; i++){
- printf("%d ",c[i]);
- getch ();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement