DATA STRUCTURE



 Assignment No-1


Set A

1) write a C program  to Count number of occurence
(or frequency) given sorted array.

Input = are []={1,1,2,2,2,2,3 3, x=2

output: 4/1/x (or 2) occurs & times in arr[]

#include<stdio.h>
void read(int a[], int n) {
    int i;
    for (i = 0; i < n; i++) {
        printf("\nEnter the number: ");
        scanf("%d", &a[i]);
    }
}
void display(int a[], int n) {
    int i;
    for (i = 0; i < n; i++)
        printf("%d\t", a[i]);
}
int count_occurrence(int a[], int n, int s) {
    int i, count = 0;
    for (i = 0; i < n; i++) {
        if (a[i] == s) {
            count++;
        }
    }
    return count;
}
int main() {
    int a[20], n, s, c;

    printf("Enter how many elements in the array: ");
    scanf("%d", &n);
    read(a, n);
    printf("\nArray elements are: ");
    display(a, n);
    printf("\nEnter search element: ");
    scanf("%d", &s);
    c = count_occurrence(a, n, s);
    if (c == 0)
        printf("\nSearch element not found");
    else
        printf("\nSearch element %d occurs %d times", s, c);
    return 0;
}
 Example Output:

Enter how many elements in the array: 9

Enter the number: 1
Enter the number: 1
Enter the number: 2
Enter the number: 2
Enter the number: 2
Enter the number: 2
Enter the number: 3
Enter the number: 3
Enter the number: 3

Array elements are: 1   1   2   2   2   2   3   3   3  

Enter search element: 2

Q2. write a c program to accept n elements, Store
those elements and Store the Square of these
numbers in another array & display both the array


#include<stdio.h>
void read(int a[], int n) {
    int i;
    for (i = 0; i < n; i++) {
        printf("\nEnter the number: ");
        scanf("%d", &a[i]);
    }
}
void display(int a[], int n) {
    int i;
    for (i = 0; i < n; i++) {
        printf("%d\t", a[i]);
    }
}
void square(int a[], int n, int b[]) {
    for (int i = 0; i < n; i++) {
        b[i] = a[i] * a[i];
    }
}
int main() {
    int a[20], b[20], n;
    printf("\nEnter how many elements in the array: ");
    scanf("%d", &n);
    printf("\nEnter array elements:");
    read(a, n);
    square(a, n, b);
    printf("\nFirst array: ");
    display(a, n);
    printf("\nSecond array (squares): ");
    display(b, n);
    return 0;
}
Example Output:

Enter how many elements in the array: 5
Enter the number: 1
Enter the number: 2
Enter the number: 3
Enter the number: 4
Enter the number: 5
First array: 1  2   3   4   5  
Second array (squares): 1   4   9   16  25  

Q3 write a C program to copy one array into
another array  

#include<stdio.h>
void read(int a[], int n) {
    int i;
    for (i = 0; i < n; i++) {
        printf("\nEnter the number: ");
        scanf("%d", &a[i]);
    }
}
void display(int a[], int n) {
    int i;
    for (i = 0; i < n; i++) {
        printf("%d\t", a[i]);
    }
}
void copy(int a[], int n, int b[]) {
    for (int i = 0; i < n; i++) {
        b[i] = a[i];  // Copy elements from array a[] to b[]
    }
}
int main() {
    int a[20], b[20], n;
    printf("\nEnter how many elements in the array: ");
    scanf("%d", &n);
    printf("\nEnter array elements:\n");
    read(a, n);
    copy(a, n, b);
    printf("\nFirst array: ");
    display(a, n);
    printf("\nSecond array (copied): ");
    display(b, n);
    return 0;
}

 Example Output:

Enter how many elements in the array: 5

Enter the number: 1
Enter the number: 2
Enter the number: 3
Enter the number: 4
Enter the number: 5

First array: 1  2   3   4   5  
Second array (copied): 1    2   3   4   5