[go: up one dir, main page]

0% found this document useful (0 votes)
4 views6 pages

1d Array

The document provides an overview of arrays in C programming, detailing their definition, syntax, and types, particularly focusing on one-dimensional arrays. It includes examples of array operations such as input/output, calculating sum and average, finding frequency of an element, identifying the largest and smallest elements, and sorting an array in ascending order. Each example is accompanied by C code snippets demonstrating the respective operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

1d Array

The document provides an overview of arrays in C programming, detailing their definition, syntax, and types, particularly focusing on one-dimensional arrays. It includes examples of array operations such as input/output, calculating sum and average, finding frequency of an element, identifying the largest and smallest elements, and sorting an array in ascending order. Each example is accompanied by C code snippets demonstrating the respective operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Arrays in C Programming

What is an Array?
An array in C is a collection of elements of the same data type, stored in contiguous memory
locations. Arrays allow storing multiple values under a single variable name, making it easier to
manage and manipulate data.

Syntax of an Array

data_type array_name[size];

data_type: The type of elements (e.g., int, float, char).

array_name: The name of the array.

size: Number of elements in the array.

Types of Arrays in C
1. One-Dimensional Array

A 1D array is a simple list of elements.

Declaration and Initialization


int arr[5] = {10, 20, 30, 40, 50}; // Declaring and initializing

Accessing Array Elements


printf("%d", arr[2]); // Outputs 30 (index starts from 0)

Compiled by: Er.Gaurab Mishra


k.m.c college, bagbazar
computer department ( HOD)
Example: Storing and Displaying an Array

#include <stdio.h>

int main() {
int arr[5] = {10, 20, 30, 40, 50};

printf("Array elements are: ");


for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}

return 0;
}

Array Operations
1. Input and Output of an Array

#include <stdio.h>

int main() {
int arr[5];

printf("Enter 5 numbers: ");


for (int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}

printf("You entered: ");


for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}

return 0;
}

C Program: Sum of All Elements in an Array

#include <stdio.h>

int main() {
int arr[5], sum = 0;

Compiled by: Er.Gaurab Mishra


k.m.c college, bagbazar
computer department ( HOD)
// Taking user input
printf("Enter 5 numbers: ");
for (int i = 0; i < 5; i++) {
scanf("%d", &arr[i]); // Storing elements in the array
}

// Calculating sum
for (int i = 0; i < 5; i++) {
sum += arr[i]; // Adding each element to sum
}

// Displaying the sum


printf("Sum of all elements: %d", sum);

return 0;
}

C Program: Average of All Elements in an Array

#include <stdio.h>

int main() {
int arr[5], sum = 0;
float average;

// Taking user input


printf("Enter 5 numbers: ");
for (int i = 0; i < 5; i++) {
scanf("%d", &arr[i]); // Storing elements in the array
}

// Calculating sum
for (int i = 0; i < 5; i++) {
sum += arr[i]; // Adding each element to sum
}

// Calculating average
average = (float)sum / 5;

// Displaying the average


printf("Average of all elements: %.2f", average);

return 0;
}

Compiled by: Er.Gaurab Mishra


k.m.c college, bagbazar
computer department ( HOD)
WAP to Find the Frequency of an Element in an Array in C

#include <stdio.h>

int main() {
int arr[100], n, i, target, frequency = 0;

// Input size of array


printf("Enter the number of elements in the array: ");
scanf("%d", &n);

// Input array elements


printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}

// Input target element to find frequency


printf("Enter the element to find its frequency: ");
scanf("%d", &target);

// Count frequency
for (i = 0; i < n; i++) {
if (arr[i] == target) {
frequency++;
}
}

// Output result
printf("Frequency of %d = %d\n", target, frequency);

return 0;
}

C Program to Find Largest and Smallest Element in an Array

#include <stdio.h>

int main() {
int arr[100], n, i;
int largest, smallest;

// Input size of the array

Compiled by: Er.Gaurab Mishra


k.m.c college, bagbazar
computer department ( HOD)
printf("Enter the number of elements in the array: ");
scanf("%d", &n);

// Input array elements


printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}

// Initialize largest and smallest to the first element


largest = smallest = arr[0];

// Loop through the array to find the largest and smallest


for (i = 1; i < n; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
if (arr[i] < smallest) {
smallest = arr[i];
}
}

// Output result
printf("Largest element = %d\n", largest);
printf("Smallest element = %d\n", smallest);

return 0;
}

C Program to Sort an Array in Ascending Order

#include <stdio.h>

int main() {
int arr[100], n, i, j, temp;

// Input size of the array


printf("Enter the number of elements in the array: ");
scanf("%d", &n);

// Input array elements


printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {

Compiled by: Er.Gaurab Mishra


k.m.c college, bagbazar
computer department ( HOD)
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}

// Selection Sort: Sorting the array in ascending order


for (i = 0; i < n - 1; i++) {
// Find the smallest element by directly comparing elements
for (j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
// Swap arr[i] and arr[j] if arr[i] is greater
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

// Output the sorted array


printf("Array elements in ascending order: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

Compiled by: Er.Gaurab Mishra


k.m.c college, bagbazar
computer department ( HOD)

You might also like