0% found this document useful (0 votes)
13 views13 pages

Array

The document provides an overview of arrays in programming, including their definition, syntax, and methods for accessing and modifying elements. It includes examples of how to create arrays, loop through them, find their size, and perform operations like calculating averages and identifying the lowest and highest numbers. Additionally, it demonstrates how to input values from the user and print negative numbers from an array.

Uploaded by

gupta.shikha9909
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views13 pages

Array

The document provides an overview of arrays in programming, including their definition, syntax, and methods for accessing and modifying elements. It includes examples of how to create arrays, loop through them, find their size, and perform operations like calculating averages and identifying the lowest and highest numbers. Additionally, it demonstrates how to input values from the user and print negative numbers from an array.

Uploaded by

gupta.shikha9909
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Array

Definition
• Arrays are used to store multiple values in a
single variable, instead of declaring separate
variables for each value.
• Syntax:
datatype variable_name[ ] = { number
separated by comma };
Example:
Int a[ ] = {10,12,14,21,56};
• Another way of creating array(syntax):
• Datatype variable_name[size];
variable_name[ ]=number;
Example:
Int a[5];
a[0]=10;
a[1]=12;
a[2]=14;
a[3]=21;
a[4]=56;
Access Array Elements
• To access an array element, refer to its index
number.
• Array indexes start with 0: [0] is the first
element. [1] is the second element, etc.
• int a[] = {25, 50, 75, 100};
printf("%d", a[0]);
Change an Array Element

• To change the value of a specific element,


refer to the index number.
• int a[] = {25, 50, 75, 100};
a[0] = 33;

printf("%d", a[0]);

// Now outputs 33 instead of 25


Loop Through an Array

• You can loop through the array elements with


the for loop.
int a[] = {25, 50, 75, 100};
int i;
for (i = 0; i < 4; i++)
{
printf("%d\n", a[i]);
}
Array Size/length
• To get the size of an array, you can use
the sizeof operator.
int a[] = {10, 25, 50, 75, 100};
int length = sizeof(a) / sizeof(a[0]);
printf("%d", length);
#include <stdio.h>
int main()
{
int num[] = {25, 50, 75, 100, 11};
int length = sizeof(num) / sizeof(num[0]);
int i;
for (i = 0; i < length; i++) {
printf("%d\n", num[i]);
}
return 0;
}
Input from User
#include <stdio.h>
int main()
{
int values[5];
printf("Enter 5 integers: ");
for(int i = 0; i < 5; ++i)
{ scanf("%d", &values[i]);
}
printf("Displaying integers: ");
for(int i = 0; i < 5; ++i)
{
printf("%d\n", values[i]);
}
return 0;
}
Find average of numbers
#include <stdio.h>
int main()
{
int b[] = {20, 22, 18, 35, 48, 26, 87, 70};
float avg, sum = 0;
int i;
int length = sizeof(b) / sizeof(b[0]);
for (i = 0; i < length; i++)
{
sum += b[i];
}
avg = sum / length;
printf("The average age is: %.2f", avg);
return 0;
}
Find lowest number in an array
#include <stdio.h>
void main()
{
int a[] = {20, 22, 18, 35, 48, 26, 87, 70};
int i;
int length = sizeof(a) / sizeof(a[0]);
int lowestnum = a[0];
for (i = 0; i < length; i++)
{
if (lowestnum> a[i])
{
lowestnum = a[i];
}
}
printf("The lowest number in the array is: %d", lowestnum);
}
Find highest no. in an array
#include <stdio.h>
int main()
{
int n;
Int arr[5] = {12,56,32,78,91};
for (int i = 1; i < n; ++i)
{
if (arr[0] < arr[i])
{
arr[0] = arr[i];
}
}
printf("Largest element = %.2lf", arr[0]);
return 0;
}
Program to print negative numbers in an
array
#include<stdio.h>
int main()
{ int Size, i, a[10];
printf("\n Enter Size of an Array : ");
scanf("%d", &Size);
printf("Enter the Array Elements : ");
for(i = 0; i < Size; i++)
{
scanf("%d", &a[i]);
}
printf("List of Negative Numbers in this Array : ");
for(i = 0; i < Size; i ++)
{
if(a[i] < 0)
{
printf("%d ", a[i]); } } return 0; }

You might also like