Structured Programming Language
1 D Array
Mohammad Imam Hossain,
Lecturer, CSE, UIU
Array
• An array is a group of like-typed variables that are
referred to by a common name.
• Arrays of any type (int, char, float, double etc.) can be
created and may have one or more dimensions.
• Arrays form a convenient way to handle groups of
related data.
Array Element
• An individual variable in the array is called an array element.
• All the array elements must be of the same type and same
storage class.
• Each array element is referred to by specifying the array name
followed by one or more subscripts, with each subscript
enclosed in square brackets.
• Each subscript must be expressed as a nonnegative integer.
• The value of each subscript can be expressed as an integer
constant, an integer variable or a more complex integer
expression.
1 Dimensional Array
• A one-dimensional array is a list of variables that are
all of the same type and are accessed through a
common name.
• C stores one-dimensional arrays in one contiguous
memory location with the first element at the lowest
address.
Declaring 1 D Array
type var_name[size];
• type is a valid C data type.
• var_name is the name of the array.
• size specifies the number of elements in
the array.
Declaring 1 D Array
type var_name[size];
• type is a valid C data type.
• var_name is the name of the array.
• size specifies the number of elements in
the array.
int x[100]; ///x is a 100 element integer array
char text[80]; ///text is an 80 element character array
#define SIZE 20
…….
float myarray[SIZE]; ///myarray is a 20 element floating-point array
1 D Array Elements
• 1st element : x[0]
• 2nd element : x[1]
• 3rd element : x[2]
• 5th element : x[4]
int x[5];
⋮
⋮
x[0] x[1] x[2] x[3] x[4]
1 D Array Elements
• 1st element : x[0]
• 2nd element : x[1]
• 3rd element : x[2]
• 5th element : x[4]
int x[5];
⋮
⋮
Element No i Index No i-1
x[0] x[1] x[2] x[3] x[4]
1 D Array Initialization
int x[5] = { 1, 2, 3, 4, 5};
Similarly,
x[0] x[1] x[2] x[3] x[4]
1 2 3 4 5
float arr[6]={0, 0.25, 0, -0.50, 0, 0};
char color[ ]={'R', 'G', 'B'};
• The array size need not be specified explicitly when initial
values are included as a part of an array definition.
• The array size will automatically be set equal to the
number of initial values included within the definition.
Assigning Values to Array Elements
int x[5] = { 1, 2, 3, 4, 5};
x[0] x[1] x[2] x[3] x[4]
1 2 3 4 5
x[2] = 7;
Assigning Values to Array Elements
int x[5] = { 1, 2, 3, 4, 5};
x[0] x[1] x[2] x[3] x[4]
1 2 3 4 5
x[0] x[1] x[2] x[3] x[4]
1 2 7 4 5
x[2] = 7;
Printing Array Elements
int x[5] = { 1, 2, 3, 4, 5};
x[0] x[1] x[2] x[3] x[4]
1 2 3 4 5
printf (“ % d “ , x[2] ) ; ///3
Problem
• Write a program that will declare a 10 elements array
and saves the value of squares from 1 to 10 to those
10 array elements.
Problem
• Write a program that will declare a 10 elements array
and saves the value of squares from 1 to 10 to those
10 array elements.
x[0] x[1] x[2] x[3] x[4]
1 4 9 16 25
x[5] x[6] x[7] x[8] x[9]
36 49 64 81 100
Solution
#include<stdio.h>
int main()
{
return 0;
}
Solution
#include<stdio.h>
#define SIZE 10
int main()
{
int sqrs[SIZE], i;
return 0;
}
Solution
#include<stdio.h>
#define SIZE 10
int main()
{
int sqrs[SIZE], i;
///assigning values to array elements
for(i=0; i<=SIZE-1; i++){
sqrs[i]=(i+1)*(i+1);
}
return 0;
}
Solution
#include<stdio.h>
#define SIZE 10
int main()
{
int sqrs[SIZE], i;
///assigning values to array elements
for(i=0; i<=SIZE-1; i++){
sqrs[i]=(i+1)*(i+1);
}
///showing the values of array elements
for(i=0; i<=SIZE-1; i++){
printf("%d th element: %dn", i, sqrs[i]);
}
return 0;
}
scanf for 1 D Array
• Using scanf you can give input values to specific array
elements.
x[0] x[1] x[2] x[3] x[4]
1 2 3 4 5
scanf("%d", &x[2]); ///let, user gives input 100
x[0] x[1] x[2] x[3] x[4]
1 2 100 4 5
Sample code
#include<stdio.h>
int main()
{
int arr[10]={1,2,3,4,5,6,7,8,9,10};
int i;
for(i=0;i<10;i+=2){
scanf("%d", &arr[i] );
}
for(i=0;i<10;i++){
printf("%d ", arr[i] );
}
return 0;
}
Note
• Single operations
which involve entire
arrays are not
permitted in C.
• So in C, you can’t
assign one entire
array to another.
#include<stdio.h>
int main()
{
int a1[10],a2[10];
int i;
for(i=0;i<10;i++){
scanf("%d", &a1[i] );
}
a2=a1; ///error
return 0;
}
Sample code
///sample code to copy one array into another
#include<stdio.h>
int main()
{
int a1[5],a2[5];
int i;
for(i=0;i<5;i++){
scanf("%d", &a1[i] );
}
///can’t write a2=a1;
for(i=0;i<5;i++){
a2[i]=a1[i];
}
///showing outputs
for(i=0;i<5;i++){
printf("%d ",a2[i]);
}
return 0;
}
Problem
• Write a program to search an element from array
elements.
Sample code
#include<stdio.h>
#define SIZE 1000
int main()
{
int arr[SIZE];
int sz,element;
printf("Please enter the number of elements of the array: ");
scanf("%d",&sz);
int i;
for(i=0;i<sz;i++){
scanf("%d",&arr[i]);
}
printf("Please enter the element to search for: ");
scanf("%d", &element);
for(i=0;i<sz;i++){
if(arr[i]==element){
printf("Yes");
break;
}
}
if(i==sz) printf("No");
return 0;
}
Problem
• Write a program to find the maximum element of an
array containing positive numbers.
Sample code
#include<stdio.h>
#define SIZE 1000
int main()
{
int arr[SIZE];
int sz, max_el=0;
printf("Please enter the number of elements of the array: ");
scanf("%d",&sz);
int i;
for(i=0;i<sz;i++){
scanf("%d",&arr[i]);
}
for(i=0;i<sz;i++){
if(arr[i]>max_el){
max_el=arr[i];
}
}
printf("The maximum value is %dn", max_el);
return 0;
}
Problem
• Write a program to save an array in reverse order
into another array.
Sample code
#include<stdio.h>
int main()
{
int arr[5]={1,2,3,4,5};
int rev_arr[5];
int i;
for(i=0;i<5;i++){
rev_arr[i]=arr[5-i-1];
}
for(i=0;i<5;i++) printf("%d ",rev_arr[i]);
return 0;
}
Problem
• Write a program to calculate the sum of two 1 D
array (element wise) of exactly same size.
Sample code
#include<stdio.h>
int main()
{
int a1[5]={1,2,3,4,5};
int a2[5]={5,4,3,2,1};
int sum[5];
int i;
for(i=0;i<5;i++){
sum[i]=a1[i]+a2[i];
}
for(i=0;i<5;i++) printf("%d ",sum[i]);
return 0;
}
Problem
• Write a program to right rotate an array. Take input
the amount to rotate right.
Sample code
#include<stdio.h>
#define SIZE 7
int main()
{
int arr[SIZE]={1,2,3,4,5,6,7};
int r_rotate;
printf("Enter the amount to rotate right: ");
scanf("%d",&r_rotate);
int i;
for(i=SIZE-1;i>=r_rotate;i--){
arr[i]=arr[i-r_rotate];
}
for(i=0;i<r_rotate;i++){
arr[i]=0;
}
for(i=0;i<SIZE;i++) printf("%d ",arr[i]);
return 0;
}
Problem
• Write a program to separate the even and odd
elements of an array into two other arrays.
Sample code
#include<stdio.h>
#define SIZE 7
int main()
{
int arr[SIZE]={1, 2, 3, 4, 5, 6, 7};
int even[SIZE],odd[SIZE];
int i,even_sz=0,odd_sz=0;
for(i=0;i<SIZE;i++){
if(arr[i]%2==0){
even[even_sz]=arr[i];
even_sz++;
}
else{
odd[odd_sz]=arr[i];
odd_sz++;
}
}
printf("Even Array: "); ///showing the even numbers
for(i=0;i<even_sz;i++) printf("%d ",even[i]);
printf("nOdd Array: "); ///showing the odd numbers
for(i=0;i<odd_sz;i++) printf("%d ",odd[i]);
return 0;
}
References:
• Teach Yourself C, 3rd edition
Section: 5.1
• Schaum’s Outline,3rd edition
Section: 9.1, 9.2

SPL 10 | One Dimensional Array in C

  • 1.
    Structured Programming Language 1D Array Mohammad Imam Hossain, Lecturer, CSE, UIU
  • 2.
    Array • An arrayis a group of like-typed variables that are referred to by a common name. • Arrays of any type (int, char, float, double etc.) can be created and may have one or more dimensions. • Arrays form a convenient way to handle groups of related data.
  • 3.
    Array Element • Anindividual variable in the array is called an array element. • All the array elements must be of the same type and same storage class. • Each array element is referred to by specifying the array name followed by one or more subscripts, with each subscript enclosed in square brackets. • Each subscript must be expressed as a nonnegative integer. • The value of each subscript can be expressed as an integer constant, an integer variable or a more complex integer expression.
  • 4.
    1 Dimensional Array •A one-dimensional array is a list of variables that are all of the same type and are accessed through a common name. • C stores one-dimensional arrays in one contiguous memory location with the first element at the lowest address.
  • 5.
    Declaring 1 DArray type var_name[size]; • type is a valid C data type. • var_name is the name of the array. • size specifies the number of elements in the array.
  • 6.
    Declaring 1 DArray type var_name[size]; • type is a valid C data type. • var_name is the name of the array. • size specifies the number of elements in the array. int x[100]; ///x is a 100 element integer array char text[80]; ///text is an 80 element character array #define SIZE 20 ……. float myarray[SIZE]; ///myarray is a 20 element floating-point array
  • 7.
    1 D ArrayElements • 1st element : x[0] • 2nd element : x[1] • 3rd element : x[2] • 5th element : x[4] int x[5]; ⋮ ⋮ x[0] x[1] x[2] x[3] x[4]
  • 8.
    1 D ArrayElements • 1st element : x[0] • 2nd element : x[1] • 3rd element : x[2] • 5th element : x[4] int x[5]; ⋮ ⋮ Element No i Index No i-1 x[0] x[1] x[2] x[3] x[4]
  • 9.
    1 D ArrayInitialization int x[5] = { 1, 2, 3, 4, 5}; Similarly, x[0] x[1] x[2] x[3] x[4] 1 2 3 4 5 float arr[6]={0, 0.25, 0, -0.50, 0, 0}; char color[ ]={'R', 'G', 'B'}; • The array size need not be specified explicitly when initial values are included as a part of an array definition. • The array size will automatically be set equal to the number of initial values included within the definition.
  • 10.
    Assigning Values toArray Elements int x[5] = { 1, 2, 3, 4, 5}; x[0] x[1] x[2] x[3] x[4] 1 2 3 4 5 x[2] = 7;
  • 11.
    Assigning Values toArray Elements int x[5] = { 1, 2, 3, 4, 5}; x[0] x[1] x[2] x[3] x[4] 1 2 3 4 5 x[0] x[1] x[2] x[3] x[4] 1 2 7 4 5 x[2] = 7;
  • 12.
    Printing Array Elements intx[5] = { 1, 2, 3, 4, 5}; x[0] x[1] x[2] x[3] x[4] 1 2 3 4 5 printf (“ % d “ , x[2] ) ; ///3
  • 13.
    Problem • Write aprogram that will declare a 10 elements array and saves the value of squares from 1 to 10 to those 10 array elements.
  • 14.
    Problem • Write aprogram that will declare a 10 elements array and saves the value of squares from 1 to 10 to those 10 array elements. x[0] x[1] x[2] x[3] x[4] 1 4 9 16 25 x[5] x[6] x[7] x[8] x[9] 36 49 64 81 100
  • 15.
  • 16.
    Solution #include<stdio.h> #define SIZE 10 intmain() { int sqrs[SIZE], i; return 0; }
  • 17.
    Solution #include<stdio.h> #define SIZE 10 intmain() { int sqrs[SIZE], i; ///assigning values to array elements for(i=0; i<=SIZE-1; i++){ sqrs[i]=(i+1)*(i+1); } return 0; }
  • 18.
    Solution #include<stdio.h> #define SIZE 10 intmain() { int sqrs[SIZE], i; ///assigning values to array elements for(i=0; i<=SIZE-1; i++){ sqrs[i]=(i+1)*(i+1); } ///showing the values of array elements for(i=0; i<=SIZE-1; i++){ printf("%d th element: %dn", i, sqrs[i]); } return 0; }
  • 19.
    scanf for 1D Array • Using scanf you can give input values to specific array elements. x[0] x[1] x[2] x[3] x[4] 1 2 3 4 5 scanf("%d", &x[2]); ///let, user gives input 100 x[0] x[1] x[2] x[3] x[4] 1 2 100 4 5
  • 20.
    Sample code #include<stdio.h> int main() { intarr[10]={1,2,3,4,5,6,7,8,9,10}; int i; for(i=0;i<10;i+=2){ scanf("%d", &arr[i] ); } for(i=0;i<10;i++){ printf("%d ", arr[i] ); } return 0; }
  • 21.
    Note • Single operations whichinvolve entire arrays are not permitted in C. • So in C, you can’t assign one entire array to another. #include<stdio.h> int main() { int a1[10],a2[10]; int i; for(i=0;i<10;i++){ scanf("%d", &a1[i] ); } a2=a1; ///error return 0; }
  • 22.
    Sample code ///sample codeto copy one array into another #include<stdio.h> int main() { int a1[5],a2[5]; int i; for(i=0;i<5;i++){ scanf("%d", &a1[i] ); } ///can’t write a2=a1; for(i=0;i<5;i++){ a2[i]=a1[i]; } ///showing outputs for(i=0;i<5;i++){ printf("%d ",a2[i]); } return 0; }
  • 23.
    Problem • Write aprogram to search an element from array elements.
  • 24.
    Sample code #include<stdio.h> #define SIZE1000 int main() { int arr[SIZE]; int sz,element; printf("Please enter the number of elements of the array: "); scanf("%d",&sz); int i; for(i=0;i<sz;i++){ scanf("%d",&arr[i]); } printf("Please enter the element to search for: "); scanf("%d", &element); for(i=0;i<sz;i++){ if(arr[i]==element){ printf("Yes"); break; } } if(i==sz) printf("No"); return 0; }
  • 25.
    Problem • Write aprogram to find the maximum element of an array containing positive numbers.
  • 26.
    Sample code #include<stdio.h> #define SIZE1000 int main() { int arr[SIZE]; int sz, max_el=0; printf("Please enter the number of elements of the array: "); scanf("%d",&sz); int i; for(i=0;i<sz;i++){ scanf("%d",&arr[i]); } for(i=0;i<sz;i++){ if(arr[i]>max_el){ max_el=arr[i]; } } printf("The maximum value is %dn", max_el); return 0; }
  • 27.
    Problem • Write aprogram to save an array in reverse order into another array.
  • 28.
    Sample code #include<stdio.h> int main() { intarr[5]={1,2,3,4,5}; int rev_arr[5]; int i; for(i=0;i<5;i++){ rev_arr[i]=arr[5-i-1]; } for(i=0;i<5;i++) printf("%d ",rev_arr[i]); return 0; }
  • 29.
    Problem • Write aprogram to calculate the sum of two 1 D array (element wise) of exactly same size.
  • 30.
    Sample code #include<stdio.h> int main() { inta1[5]={1,2,3,4,5}; int a2[5]={5,4,3,2,1}; int sum[5]; int i; for(i=0;i<5;i++){ sum[i]=a1[i]+a2[i]; } for(i=0;i<5;i++) printf("%d ",sum[i]); return 0; }
  • 31.
    Problem • Write aprogram to right rotate an array. Take input the amount to rotate right.
  • 32.
    Sample code #include<stdio.h> #define SIZE7 int main() { int arr[SIZE]={1,2,3,4,5,6,7}; int r_rotate; printf("Enter the amount to rotate right: "); scanf("%d",&r_rotate); int i; for(i=SIZE-1;i>=r_rotate;i--){ arr[i]=arr[i-r_rotate]; } for(i=0;i<r_rotate;i++){ arr[i]=0; } for(i=0;i<SIZE;i++) printf("%d ",arr[i]); return 0; }
  • 33.
    Problem • Write aprogram to separate the even and odd elements of an array into two other arrays.
  • 34.
    Sample code #include<stdio.h> #define SIZE7 int main() { int arr[SIZE]={1, 2, 3, 4, 5, 6, 7}; int even[SIZE],odd[SIZE]; int i,even_sz=0,odd_sz=0; for(i=0;i<SIZE;i++){ if(arr[i]%2==0){ even[even_sz]=arr[i]; even_sz++; } else{ odd[odd_sz]=arr[i]; odd_sz++; } } printf("Even Array: "); ///showing the even numbers for(i=0;i<even_sz;i++) printf("%d ",even[i]); printf("nOdd Array: "); ///showing the odd numbers for(i=0;i<odd_sz;i++) printf("%d ",odd[i]); return 0; }
  • 35.
    References: • Teach YourselfC, 3rd edition Section: 5.1 • Schaum’s Outline,3rd edition Section: 9.1, 9.2