C-Notes Module 4
C-Notes Module 4
Array
Array is the group of identical elements organized in a single variable. An array is a
collection of data that holds fixed number of values of same type. For example: if you want
to store marks of 100 students, you can create an array for it.
int mark[100];
i. One-dimensional arrays
int mark[5] = {19, 10, 8, 17, 9};
Arrays have 0 as the first index not 1. In this example, mark[0]
If the size of an array is n, to access the last element, (n-1) index is used. In this example,
mark[4]
/* Output */
Large = 136
char ch[10]={'c', 'o', 'm', 'p', 'u', 't', 'e', 'r', ''\0'};
or
char ch[10]="computer”
As we know, array index starts from 0, so it will be represented as in the figure given below.
ch
Index 0 1 2 3 4 5 6 7 8
Value c o m p u t e r \0
Example
Let's see an example of counting the number of vowels in a string.
#include<stdio.h>
void main ()
{
char s[11] = "computer";
int i = 0;
int count = 0;
while(i<11)
{
if(s[i]=='a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'u' || s[i] == 'o')
{
count ++;
}
i++;
}
printf("The number of vowels %d",count);
}
Output
The number of vowels 3
#include<stdio.h>
struct Point
{
int x, y;
};
void main()
{
struct Point p1 = {0, 1};
/* Accessing members of point p1 */
p1.x = 20;
printf ("x = %d, y = %d", p1.x, p1.y);
}
Output:
x = 20, y = 1
Example
struct Point
{
int x, y;
};
void main()
{
// Create an array of structures
struct Point p[10];
In the below example student structure having id, name and percentage attributes. record
is the variable to control entire structure.
Example:
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
int main()
{
struct student record;
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
func(record);
return 0;
}
OUTPUT:
Id is: 1
Name is: Raju
Percentage is: 86.5