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

Module 3 (Arrays 1) - 16857900840503 PDF

The document provides an overview of arrays in C programming, detailing their definition, types (one-dimensional, two-dimensional, and multidimensional), and initialization methods. It explains how to determine the size of an array based on data types and includes examples of reading and writing single-dimensional arrays. The content serves as a guide for understanding and implementing arrays in problem-solving scenarios.
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)
3 views6 pages

Module 3 (Arrays 1) - 16857900840503 PDF

The document provides an overview of arrays in C programming, detailing their definition, types (one-dimensional, two-dimensional, and multidimensional), and initialization methods. It explains how to determine the size of an array based on data types and includes examples of reading and writing single-dimensional arrays. The content serves as a guide for understanding and implementing arrays in problem-solving scenarios.
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

C Programming For Problem Solving

(18CPS13)

MODULE 3:

Arrays
What is Array? Types of Arrays
1. One-dimensional arrays
•An array is a fixed-size sequential collection of elements of 2. Two-dimensional arrays
same data types that share a common name. 3. Multidimensional arrays
•It is simply a group of data types.
•An array is a derived data type. One-dimensional Arrays
•An array is used to represent a list of numbers , or a list of • It is a linear list consisting of related data items.
names.
• In memory, all the data items are stored in
For example,an array can be used contiguous memory location i.e. one after the
•List of employees in an organization. other.
•Test scores of a class of students. • Pictorial representation of single dimensional
array is given below.
• List of customers and their telephone numbers.
int a[6];
•List of students in the college.
For Example, Consider an array of marks of 5 students as
shown below

Marks[0] Marks[1] Marks[2] Marks[3] Marks[4]subscript


The size of an Array
The size of an array depends on the type
of the data items (Data types)stored and number
of items (index or subscript value).
In general

Array Size=n*sizeof(data type)

nis the number of data items


data type  the data type of data items

For example

int marks[5];

Array Size=5*2=10bytes
n items
2n if size of integer is 2
4n if size of integer is 4
Initialization of single dimensional arrays Initializing all specified memory locations

Datatype Array_Name[size]={list of values}; int a[5]={10,15,20,25,30}; a[0] a[1] a[2] a[3] a[4]
10 15 20 25 30
Where
• Data type can be int, float, char etc char b[6]={„C‟,‟O‟,‟M‟,‟P‟,‟U‟,‟T‟,‟E‟};
•Array_name is the name of the array
•Size can be an expression should be evaluated to a positive Partial array Initialization
integer only or can be integer constant.
•List of values are array elements enclosed within ‘{‘ and ‘}’ int a[5]={10,15}; a[0] a[1] a[2] a[3] a[4]
separated by commas. 10 15 0 0 0

char b[7]={„C‟,‟O‟,‟M‟,‟P‟,‟U‟};
The various ways of initializing arrays are:
•Initializing all specified memory locations Initialization without array size
•Partial array initialization
•Initialization without size int a[]={10,15,20,25,30};
•String initialization char b[]={„C‟,‟O‟,‟M‟,‟P‟,‟U‟,‟T‟,‟E‟};

Array Initialization with a string


char b[]=”COMPUTER”;
b[o] b[1] b[2] b[3] b[4] b[5] b[6] b[7] b[8]
C O M P U T E R \n
/*Program to read n elements from the keyboard and to display
Reading/writing single dimensional array n elements on the monitor*/
#include<stdio.h>
Example:
void main()
int a[5]; a[0] through a[4] {
int n,i,a[10];
To read printf(“Enter the number of items\n”);
for(i=0;i<n;i++) i=0 1 2 n-1 scanf(“%d”,&n); //5
{ a[0] a[1] a[2] … a[n-1] printf(“Enter the n elements\n”);
scanf(“%d”,&a[i]); 10 20 30 50 for(i=0;i<n;i++) i 0 1 2 3 4

} { A[0] A[1] A[2] A[3] A[4]


scanf(“%d”,&a[i]); 10 20 30 40 50
}
To write printf(“The n elements are\n”);
for(i=0;i<n;i++) for(i=0;i<n;i++)
{ {
printf(“%d”,a[i]); printf(“%d”,a[i]);
} }
getch();
}
OUTPUT:
Enter the number of items
5
Enter the n elements
10 20 30 40 50
The n elements are
10 20 30 40 50

You might also like