An array is a container object that holds a fixed number of values of a single type. Basically A collection of similar data types stored at contiguous memory locations. The length of an array is established when the array is created. After creation, its length is fixed.

Syntax :

int marks[3];
char name[10];
float price[2];

Languages like C and C++ and java are 0-based indexing where the number string system starts from 0 and not 1. This is called zero-based indexing. To input variable and print it the syntax is

scanf("%d",&marks[0]);
printf("%d",marks[0]);

When you take a size higher than the size of the array for printing or scanning the compiler won't find out there will be a run time error. Run time error= When you run the program the error shown is called run time error.

Q1 Write a program to enter price of 3 items and print their final cost with gst in C.

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


int main(){
 float price[3];
 printf("Enter the cost of the items bought:\n");
 scanf("%f",&price[0]);
  scanf("%f",&price[1]);
   scanf("%f",&price[2]);

   printf("total price 1:%f\n", price [0]+(0.18*price[0]));
   printf("total price 2:%f\n", price [1]+(0.18*price[1]));
   printf("total price 3:%f\n", price [2]+(0.18*price[2]));
  return 0;
}

Initialization of Array

For an array its optional to specify the size of an array in the syntax.

Pointer Arithmetic

The pointer can be incremented and decremented.

Example :

int age=22;
 int *ptr=&age;
 printf("ptr=%u\n",ptr);
 ptr++;
 printf("ptr=%u\n",ptr);
 ptr--;
 printf("ptr=%u\n",ptr);

        Output

ptr=6422296     at first it increased by 4
ptr=6422300    then it decreased cuz of --
ptr=6422296

        Same code with float

float age=22.00;
 float *ptr=&age;
 printf("ptr=%u\n",ptr);
 ptr++;
 printf("ptr=%u\n",ptr);
 ptr--;
 printf("ptr=%u\n",ptr);

        Same code with char

char age='*';
 char *ptr=&age;
 printf("ptr=%u\n",ptr);
 ptr++;
 printf("ptr=%u\n",ptr);
 ptr--;
 printf("ptr=%u\n",ptr);

output
ptr=6422299
ptr=6422300
ptr=6422299

Pointer Arithmetic

-We can also subtract one pointer from another

-We can also compare 2 pointers

Example :

int main(){
 int age=22;
 int _age='a';
 int *ptr=&age;
 int *_ptr=&_age;

 printf("%u,%u,difference=%u\n",ptr,_ptr,ptr-_ptr);
 _ptr=ptr;
 printf("comparision=%u\n",ptr==_ptr);
  return 0;
}

This prints the difference between the 2 variables.

Array is a Pointer

In an array ,you can store that pointer into any pointer variable of the correct type.

int *ptr =&arr[0];

int *ptr=arr;

int aadhar[5];

 int *ptr=&aadhar[0];
 //input
 for (int i=0;i<5;i++)
 {
  printf("%d,index=",i);
  scanf("%d",(ptr+i));
 }
//output
 for(int i=0;i<5;i++)
 {
  printf("%d index=%d\n",i+1,*(ptr+i));
 }
  return 0;

Array as a Function Argument

You can pass the array to a function to make it accessible within the function.

// function declaration

void printNumbers(int arr[], int n); or

void printNumbers(int*arr, int n);

//function call

print numbers

Example :

void printnumbers(int arr[],int n);
int main(){
 int arr[]={1,2,3,4,5,6};
 printnumbers(arr,6);
  return 0;
}
void printnumbers(int arr[],int n)
{
  for (int i=0;i<n;i++)
  {
    printf("%d \t",arr[i]);
  }
  printf("\n");
}

Multidimensional Arrays

A multidimensional array is an array of arrays. Multidimensional arrays are useful when you want to store data as a tabular form, like a table with rows and columns.

arr[0] [0]

2D arrays

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

//Access

arr [0] [1]

arr[1][0]

arr[1][1]

Q2 Write a program to print said marks in C.

int main(){
  // 2*3
  int marks[2][3];// _ _ _| _ _ _
  marks[0][0]=90;
  marks[0][1]=89;
  marks[0][2]=78;

  marks[0][0]=91;
  marks[0][1]=88;
  marks[0][2]=70;

  printf("The answer is\t=%d\n",marks[0][0]);
  return 0;
}

Q3 Write a function to count the number of odd numbers i an array in C.

int countOdd(int arr[],int n);
int main(){
  int count=0;
  int arr[]={1,2,3,4,5,6,7,8,9,10};
  printf("deamon value %d\n",countOdd(arr,10));
  for(int i=0;i<10;i++){
   printf("%d\t\n",arr[count]);
   count++;
  }
  return 0;
}

int countOdd(int arr[],int n){
  int count=0;
  for(int i=0;i<n;i++)
  {
    if(arr[i]%2!=0){

      count++;
    }
  }
  return count;
}

Q4 Write a program to input 5 numbers and make them reverse in C.

int reverse(int arr[],int n);
void printarr(int arr[],int m);
int main(){
  int arr[5];
  printf("Enter 5 values\n");
   scanf( "%d %d %d %d %d", &arr[0], &arr[1], &arr[2], &arr[3],&arr[4]);
  reverse(arr,5);
  printarr(arr,5);
  return 0;
}
void printarr(int arr[],int m)
{
  for(int i=0;i<m;i++)
  {
    printf("%d\t",arr[i]);
  }
  printf("\n");
}

int reverse(int arr[],int n)
{
  for(int i=0;i<n/2;i++)
  {
    int firstval=arr[i];
    int secondval=arr[n-i-1];
    arr[i] = secondval;
    arr[n-i-1] = firstval;

  }
}

The same program but it can be performed for n numbers of numbers

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

int countOdd(int arr[],int n);
int reverse(int arr[],int n);
void printarr(int arr[],int m);
int main(){
  int n;
  printf("\tEnter the number of numbers you are going to type\n");
  scanf("\t%d",&n);
  int arr[n];
  printf("\tEnter n values\n");
  for(int i=0;i<n;i++){
   scanf( "%d", &arr[i]);
  }
  reverse(arr,n);
  printarr(arr,n);
  return 0;
}
void printarr(int arr[],int m)
{
  for(int i=0;i<m;i++)
  {
    printf("%d\t",arr[i]);
  }
  printf("\n");
}

int reverse(int arr[],int n)
{
  for(int i=0;i<n/2;i++)
  {
    int firstval=arr[i];
    int secondval=arr[n-i-1];
    arr[i] = secondval;
    arr[n-i-1] = firstval;

  }
}

Q4 Write a program to input a number and print the n number of the Fibonacci number in C.


#include<stdio.h>
#include<math.h> 
#include <time.h>


int main(){
  int n;
  printf("/t enter n(n>2): \n");
  scanf("%d",&n);

  int fib[n];
  fib[0]=0;
  fib[1]=1;

  for(int i=2;i<n;i++)
  {
    fib[i]=fib[i-1]+fib[i-2];
    printf("%d \t",fib[i]);
  }
  printf("\n");
  return 0;
}

Nature's patterns: Golden spirals and branching fractals - CNET

Q5 Create a 2d array,storing teh talbes of 2&3 in C.

void storeTable(int arr[][10], int n, int m, int number);

int main(){
 int tables[2][10];
 storeTable(tables,0,10,2);
 storeTable(tables,1,10,3);

for(int i=0;i<10;i++){
printf("\t%d",tables[0][i]);
}
printf("\n");
for (int j=0;j<10;j++)
{
  printf("\t%d",tables[1][j]);
}
  return 0;
}
 void storeTable(int arr[][10],int n,int m,int number){
  for(int i=0;i<m;i++)
  {
    arr[n][i]=number*(i+1);
  }
 }

Hope you enjoyed learning about Arrays, this helps us as the base of all sorting algorithms, hope this blog was useful. And don't forget to follow and share this blog with a friend who wants to learn about programming.

Thank you...