How to perform operations involving external variables

Rumman Ansari   2017-11-13   Student   Data structure > array   1166 Share

Sometimes, you will have to multiply the array elements by an external variable or constant. One very clear example of this is mangifying the value of a array in all three coordinates. A function can be written in C/C++ that will allow doing any mathematical operation involving array elements and an external constant or a variable. Here is the code in C.

Program

Program to multiply each array Elements with a constant 2.

 

// How to multiply array Elements with a constant

#include<stdio.h> 
void main()
{
int array[] ={0,1,2,3,4,5,6,7,8,9,10,11};  
ExtOp(array,2,0,11);   // function calling
}

 
void ExtOp(int array[],int MyCon,int start,int finish)
{
    int i=start;
    for(i=0;i<=finish;i++)
    {
        //multiplication with every element
        array[i]*= MyCon;
        printf("%d ",array[i]);
    }
}

Output

If you will run the above program it will prodce the following result.

 
0 2 4 6 8 10 12 14 16 18 20 22 

Program

Program to add each array Elements with a constant 10.

 

// How to add array Elements with a constant

#include<stdio.h> 
void main()
{
int array[] ={0,1,2,3,4,5,6,7,8,9,10,11};  
// Here the constant vale is 10
ExtOp(array,10,0,11);   // function calling
}



void ExtOp(int array[],int MyCon,int start,int finish)
{
    int i=start;
    for(i=0;i<=finish;i++)
    {
        //multiplication with every element
        array[i]+= MyCon;
        printf("%d ",array[i]);
    }
}

Output

If you will run the above program it will prodce the following result

 
10 11 12 13 14 15 16 17 18 19 20 21 

How to add the elements of an array

Program

Program to add all the array elements.

 
//Program to multiply all the array elements

#include<stdio.h> 
void main()
{
int array[10],n, i, value;
printf("Enter How many elemnts you want to insert in array: ");
scanf("%d",&n) ;
for(i= 0; i<n; i++)
{
   printf("array[%d]: ",i);	
  scanf("%d",&array[i]);
}

value = addelements(array,n);   // function calling
printf("Add value = %d \n",value);
}




int addelements(int array[],int size)
{
	int i,add;
	add=0;
    for(i=0;i< size; i++){ 
        add+=array[i];
    }
    return add;
}

Output

If you will run the above program it will prodce the following result

 
Enter How many elemnts you want to insert in array: 4
array[0]: 1
array[1]: 2
array[2]: 3
array[3]: 4
Add value = 10
Press any key to continue . . .
 

How to multiply the elements of an array

Program

Program to multiply all the array elements.

 
//Program to multiply all the array elements

#include<stdio.h> 
void main()
{
int array[10],n, i, value;
printf("Enter How many elemnts you want to insert in array: ");
scanf("%d",&n) ;
for(i= 0; i<n; i++)
{
   printf("array[%d]: ",i);	
  scanf("%d",&array[i]);
}

value = multiplyelements(array,n);   // function calling
printf("Multiplication value = %d \n",value);
}




int multiplyelements(int array[],int size)
{
	int i,mult;
	mult=1;
    for(i=0;i< size; i++){ 
        mult*=array[i];
    }
    return mult;
}

Output

If you will run the above program it will prodce the following result

 
Enter How many elemnts you want to insert in array:
4
array[0]: 1
array[1]: 2
array[2]: 3
array[3]: 4
Multiplication value = 24
 


See our other blogs on data structure

How to add array Elements in odd and even places
How to add array Elements in a special region
C Program to Implement a Queue using an Array
Hashing, Hash table, hash function, collision and collision resolution technique
C Program to Construct a Tree with insertion operation and display the output