

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















ARRAYS A regular variable stores a single data An array stores multiple data of similar type Each data in an array is accessed by an index ( Starts from 0)
ARRAY DECLARATION []; Example: float cgpa[100]; ARRAY ELEMENT ACCESS Index 0 1 2 3 4 5 6 7 8 9 Data 4.0 3.9 3.7 3.8 3.8 3.2 2.9 3.9 3.5 3. float cgpa[10]; cgpa[0] = 4.0; scanf(“%f”, &cgpa[2]); printf(“%f\n”, cgpa[4] // output is 3.
#include int main() { int a[10000]; int sum = 0, i; for(i = 0; i < 10000; i++) scanf("%d", &a[i]); for(i = 0; i < 10000; i++) sum += a[i]; printf("The sum is %d\n", sum); return 0; }
#include int main() { int n, i, j, temp; scanf("%d", &n); int A[n]; for(i = 0; i < n; i++) { scanf("%d", &A[i]); } for(i = 0; i < n - 1; i++) { for(j = 0; j < n - i - 1; j++) { if(A[j] > A[j + 1]) { temp = A[j];
A[j] = A[j + 1]; A[j + 1] = temp; } } } for(i = 0; i < n; i++) { printf("%d\n", A[i]); } return 0; } 8 7 8 1 3 2 6 4 3 1 2 3 3 4 6 7 8 Input OUTPUT
5 1 2 3 4 5 5 4 3 2 1 Input output
3 .WAP that will take n integer numbers into an array, and then find the maximum -minimum among them with its index position. #include int main() { int n, i; scanf("%d", &n); int arr[n]; for(i = 0; i < n; i++) { scanf("%d", &arr[i]); } int max = arr[0], min = arr[0]; int maxIndex = 0, minIndex = 0; for(i = 1; i < n; i++) { if(arr[i] > max) { max = arr[i]; maxIndex = i;
4 WAP that will take n integer numbers as input in an array and then insert a number in a position specified by the user in the array. #include int main() { int n, i, pos, num; scanf("%d", &n); int arr[n + 1]; for(i = 0; i < n; i++) { scanf("%d", &arr[i]); } scanf("%d", &pos); scanf("%d", &num); for(i = n; i > pos; i--) { arr[i] = arr[i - 1]; }
arr[pos] = num; for(i = 0; i <= n; i++) { printf("%d ", arr[i]); } return 0; } 5 10 20 30 40 50 2 25 10 20 25 30 40 50 Input Output
Input output 5 1 2 3 4 5 6 7 8 9 10
Now remove all duplicates numbers from that array.
#include int main() { int n, i, j, k; scanf("%d", &n); int A[n]; for(i = 0; i < n; i++) { scanf("%d", &A[i]); } for(i = 0; i < n; i++) { for(j = i + 1; j < n; ) { if(A[i] == A[j]) { for(k = j; k < n - 1; k++) { A[k] = A[k + 1]; }
7 WAP that will take n integers into array A and m positive integers into array B. Now find the intersection (set operation) of array A and B. #include int main() { int n, m, i, j; scanf("%d", &n); int A[n]; for(i = 0; i < n; i++) { scanf("%d", &A[i]); } scanf("%d", &m); int B[m]; for(i = 0; i < m; i++) { scanf("%d", &B[i]); } for(i = 0; i < n; i++) { for(j = 0; j < m; j++)
Input Output 5 1 2 3 4 5 4 3 4 6 7 3 4