Skip to content

Latest commit

 

History

History

chapter07

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Chapter 7- Arrays

An array is a collection of similar elements.

one variable => Capable of storing multiple similar values.

syntax-

int marks[90];  // integer array
char name[20];  // character array or string
float percentile[90]; // float array

Note:--- Array Index starts with 0.
1.Write a program to accept marks in 4 subjects by the user in an array and print them to the screen one by one.

2.Write a program to accept marks of 5 students in an array and print them to the screen one by one.

int a[3]={34,45,20};
arrays can be initialized while declaration

Arrays in memory

int arr[3] = {1,2,3}

1 integer = 4 bytes -- this is askable question.
this will reserve 4 x3 = 12 bytes.

Pointer Arithmetic

A pointer can be incremented to point to the next memory location of that type.

Following operations can be performed on a pointer:-

  1. Addition of a number to a pointer.
  2. Substraction of a number from a pointer.
  3. Substraction of one pointer from another.
  4. Comparison of two pointer variables.

Accessing Arrays using pointers

if the pointer variable(pv) points to index 0, pv++ will point to index 1 & so on......

Passing arrays to functions

void printarray(int *ptr,int n)
OR
void printarray(int ptr[],int n)

Multidimensional Arrays

An array can be of 2 dimension/3 dimension/n dimensions.

A 2 dimensional array can be defined as


int arr[3][2] = {{1,4}
                 {7,9}
                 {11,22}};

Exercises

  1. Create an array of 10 numbers. Verify using pointer arithmetic that (ptr+2) points to the third element where ptr is a pointer pointing to the first element of the array.

  2. Write a program to create an array of 10 integers and store multiplication table of 5 in it.

  3. Repeat problem 2 for general input provided by the user using scanf.

  4. Write a program containing a function which reverses the array passed to it.

  5. Write a program containing functions which counts the number of positive integers in an array.

  6. Create an array of size 3x10 containing multiplication tables of the numbers 2,7 and 9 respectively.

  7. Repeat problem 7 for a custom input given.

  8. create a 3-D array and print the address of its elements in increasing order.

  9. create an array of size n, input the elements of the array, sum them and print the sum of the elements in a new line.