0% found this document useful (0 votes)
19 views25 pages

Arrays in C-1D

This document provides an overview of arrays in C programming, including their definition, types, and real-life applications. It explains how to declare and manipulate one-dimensional arrays, along with examples of programs that find the largest number in an array and separate even and odd elements. Additionally, it includes quizzes to test understanding of array concepts in C.
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)
19 views25 pages

Arrays in C-1D

This document provides an overview of arrays in C programming, including their definition, types, and real-life applications. It explains how to declare and manipulate one-dimensional arrays, along with examples of programs that find the largest number in an array and separate even and odd elements. Additionally, it includes quizzes to test understanding of array concepts in C.
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
You are on page 1/ 25

Arrays

Arrays in C
1. Through this lecture, students will understand
Learning the concept of arrays, types of arrays,
Outcomes application in real life
2. Students will learn how to declare arrays in C
Contents
• What is Array?
• Real life examples
• Types of array -1D
• How to declare 1D arrays
Some Real life Arrays
How comes an array in C?
main( )
{ int x ;
x will take value as 10
x=5;
x = 10 ;
printf ( "\nx = %d", x ) ;
} How to store more
than one value at a
time in a single
variable?
Two alternatives to a question?
Scenario: Suppose we wish to arrange the percentage marks obtained by 100
students in ascending order.

Alternative 1: Construct 100 variables to store percentage marks obtained by 100


different students, i.e. each variable containing one student’s marks.

Alternative 2: Construct one variable (called array or subscripted variable) capable of


storing or holding all the hundred values.
What is an array?

collection of data, which


is very similar to a matrix
defines the way of
kind of data structure but a difference is that
arranging the data
array can hold only data
of similar datatypes

Iterating the arrays using


cannot alter the size of allocates memory in
their index is faster
the array once array is contiguous memory
compared to any other
declared. locations for its elements.
methods like linked list

Inserting and deleting the


records from the array
would be costly
Syntax:
data_type arrayname[index];

Index value:
size of the array,
starts from zero

Valid array declarations Invalid array declarations

int arr[5] = {1, 2, 3, 4, 5};


char arr[5] = {'a', 'b', 'c', 'd', 'e'}; char arr[5] = {a, b, c, d, e};
char arr[5] = {'a', 98, 'c', 100}; int arr[5] = { '1', '2', '3', '4', '5'}
float arr[3] = {1.0, 2.5, 3.9}; char arr[2] = {"Invalid", "array"} ;
Program without array and with array

#include <stdio.h> #include <stdio.h>


int main() int main()
{ int s1, s2, s3; { int s[3], i;
printf ("Enter students marks details "); printf ("Enter students marks details ");
printf ("\ns1 = "); for(i = 0; i < 3; i++)
scanf ("%d", &s1); {
printf ("\ns2 = "); printf ("\n%d = ", i + 1);
scanf ("%d", &s2); scanf ("%d",&s[i]);
printf ("\ns3 = "); }
scanf ("%d", &s3); printf ("\n---Students marks details--- ");
printf ("\n---Students marks details---\n "); for(i = 0; i < 3; i++)
printf ("s1 = %d\n", s1); { printf ("\ns%d = %d ", i + 1, s[i]); }
printf ("s2 = %d\n", s2); return 0;
printf ("s3 = %d\n", s3); }
return 0; }
One dimensional Array (1-D array)
int count Enough memory for 1 int
12345

float price
Enough memory for 1 float

56.981

char letter Enough memory for 1 char


A

11
Syntax: data_type varname[size];
Declaring Arrays
• When declaring arrays, specify
– Name
– Type of array
– Number of elements
arrayType arrayName[ numberOfElements ];
– Examples:
int c[ 10 ];
float myArray[ 3284 ];
• Declaring multiple arrays of same type
– Format similar to regular variables
– Example:
int b[ 100 ], x[ 27 ];
Program to Find the Largest Number
int main()
in an Array
{
int array[50], size, i, largest;
printf("\n Enter the size of the array: ");
scanf("%d", &size);
printf("\n Enter %d elements of the array: ", size);
for (i = 0; i < size; i++)
scanf("%d", &array[i]);
largest = array[0];
for (i = 1; i < size; i++)
{
if (largest < array[i])
largest = array[i];
}
printf("\n largest element present in the given array is : %d", largest);
return 0;
}
• C Program to Find the Largest
Two Numbers in a given Array
• C program to reverse an array
Implement • Calculate the average of n
following numbers, then compute
deviation of each number to an
Programs average
Find the Largest Two Numbers in a given Array
#include <stdio.h>
#define MAX 4 if (larg1 < larg2)
void main() { temp = larg1;
{ int arr[MAX], i, larg1, larg2, temp; larg1 = larg2;
for (i = 0; i < MAX; i++) larg2 = temp; }
{ for (i = 2; i < 4; i++)
scanf("%d", &arr[i]); {
}
if (arr[i] >= larg1)
/*input integers*/
{
larg2 = larg1;
for (i = 0; i < MAX; i++)
larg1 = arr[i];
{
}
printf("%d", arr[i]);
else if (arr[i] > larg2)
}
{
/* assume first element as the first larges t*/
larg2 = arr[i];
larg1 = arr[0];
}}
/* assume next element as the second largest */
printf("%d first largest \n", larg1);
larg2 = arr[1]; printf("%d is the second largest ", larg2);
}
Put Even & Odd Elements of an Array in 2
Separate Arrays
#include <stdio.h> else
void main() {
{int arr[ ], odd_arr[ ], even_arr[ ]; Odd_arr[k] = arr[i];
int i, j = 0, k = 0, n; k++;
printf("Enter the size of array \n"); }
scanf("%d", &n); }
printf("Enter the elements of the array \n"); printf("The elements of Odd array are \n");
for (i = 0; i < n; i++) for (i = 0; i < j; i++)
{scanf("%d", &arr[i]);} {
/* Copy odd and even elements into their respective printf("%d\n", odd_arr[i]);
arrays */
}
for (i = 0; i < n; i++)
printf("The elements of even array are \n");
{if (arr[i] % 2 == 0)
for (i = 0; i < k; i++)
{
{
even_arr[j] = arr[i];
printf("%d\n", even_arr[i]);
j++;
}}
}
Sort the Array in an Ascending
Order
#include <stdio.h> if (number[i] > number[j])
{
void main() a = number[i];
{ number[i] = number[j];
int i, j, a, n, number[30];
number[j] = a;
}
printf("Enter the value of N \n");
scanf("%d", &n);
}
printf("Enter the numbers \n"); }
for (i = 0; i < n; ++i)
scanf("%d", &number[i]); printf("The numbers arranged in
for (i = 0; i < n; ++i) ascending order are given below \n");
{ for (i = 0; i < n; ++i)
for (j = i + 1; j < n; ++j) printf("%d\n", number[i]);
{ }
Quiz
1.int main()
{ int i;
int arr[5] = {1};
for (i = 0; i < 5; i++)
printf("%d ", arr[i]);
return 0;
}
(A)1 followed by four garbage values (B) 1 0 0 0 0 (C) 1 1 1 1 1 (D) 0 0 0 0 0

2. #include <stdio.h>
int main()
{ int arr[5];
// Assume that base address of arr is 2000 and size of integer is 32 bit
arr++;
printf("%u", arr);
return 0; }
A) 2002 B) 2004 C) 2020 D) lvalue required
3. #include <stdio.h>
int main()
{ int arr[5];
// Assume base address of arr is 2000 and size of integer is 32 bits
printf("%u %u", arr + 1, &arr + 1);
return 0;
}
A) 2004 2020 B) 2004 2004 C) 2004 Garbage value
D) The program fails to compile because Address-of operator cannot be used with array name

4. # include <stdio.h>
void print(int arr[])
{ int n = sizeof(arr)/sizeof(arr[0]);
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]); }
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
print(arr); return 0; }
A) 1, 2, 3, 4, 5, 6, 7, 8 B) Compiler Error C) 1 2 D) Run Time Error
5. Consider the following C-function in which a[n] and b[m] are two sorted integer
arrays and c[n + m] be another integer array.
void xyz(int a[], int b [], int c[]){
int i, j, k;
i = j = k = O;
while ((i<n) && (j<m))
if (a[i] < b[j]) c[k++] = a[i++];
else c[k++] = b[j++]; }
Which of the following condition(s) hold(s) after the termination of the while
loop? (i) j < m, k = n+j-1, and a[n-1] < b[j] if i = n (ii) i < n, k = m+i-1, and b[m-1] <=
a[i] if j = m
A) only (i) B) only (ii) C) either (i) or (ii) but not both D) neither (i) nor (ii)
6. Which of the following is true about arrays in C.
A) For every type T, there can be an array of T.
B) For every type T except void and function type, there can be an array of T.
C) When an array is passed to a function, C compiler creates a copy of array.
D)2D arrays are stored in column major form

7. int main()
{ int i;
int arr[5] = {1};
for (i = 0; i < 5; i++)
printf("%d ", arr[i]);
return 0; }
A) 1 followed by four garbage values B) 1 0 0 0 0 C) 1 1 1 1 1 D) 0 0 0 0 0
8. #include "stdio.h“
int size = 4;
int arr[size];
int main() {
if(arr[0])
printf("Initialized to ZERO");
else
printf("Not initialized to ZERO");
return 0; }
A) No compile error and it’ll print “Initialized to ZERO”.
B) No compile error and it’ll print “Not initialized to ZERO”.
C) Compile error because size of arr has been defined using variable outside any function.
D) No compile error and it’ll print either “Initialized to ZERO” or “Not initialized to ZERO”
depending on what value is present at arr[0] at a particular run of the program.
Thank You......

You might also like