0% found this document useful (0 votes)
9 views

Modified_code_in_which_I_have_taken_a_array_A__and_stored_square_of_all_elements_in_array_B[1]

c++

Uploaded by

rayyanquddusi683
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Modified_code_in_which_I_have_taken_a_array_A__and_stored_square_of_all_elements_in_array_B[1]

c++

Uploaded by

rayyanquddusi683
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Modified LAB 3 and so on:

CODE IN WHICH ARRAY B IS MADE FROM SQAURE OF ARRAY A:


/*Basically in this program i have taken a integer x and there are x

elements in array A and there are also x elements in array B . Now I am taking square of each element of
A array and storing it in B array and new array that is square of array A is formed which was the required
program....*/

#include<stdio.h>

int main(){

int x;

printf("How many elements do you want to create an array ");

scanf("%d",&x);

int a[x];

int b[x];

printf("Now enter the elements of array A ");

for(int i=0;i<x;i++){

scanf("%d",&a[i]);

printf("The array A is\n");

for(int i=0;i<x;i++)

printf("%d ",a[i]);

for(int i=0;i<x;i++)

b[i]=a[i]*a[i];

printf("\nThe array B is\n");

for(int i=0;i<x;i++)
{

printf("%d,",b[i]);

return 0;

CODE TO FIND AVERAGE OF ALL ELEMENTS IN A ARRAY :


#include<stdio.h>
int main(){
int x;
printf("Good morning hope you are doing fine .\nPlease tell of how many elements array you want to
create \n");
scanf("%d",&x);
int a[x];
printf("Now enter the elements please \n");
for(int i=0;i<x;i++)
{
scanf("%d",&a[i]);
}
int sum=0;
for(int i=0;i<x;i++)
{
sum=sum+a[i];
}
float avg=sum/x;
printf("So the required average of all elements in array is %f ",avg);
return 0;
}
CODE TO STORE EVEN NUMBERS IN EVEN ARRAY AND ODD
NUMBERS IN ODD ARRAY :
#include<stdio.h>
int main(){
int even[5];
int odd[5];
int num;
int evencount=0;
int oddcount=0;
printf("Enter the elements\n");
for(int i=0;i<5;i++)
{
scanf("%d",&num);
if(num>0)
{
if(num%2==0)
{
even[evencount]=num;
evencount++;
}
else
{
odd[oddcount]=num;
oddcount++;
}
}
else{
printf("%d is a invalid input\n",num);
}
}
printf("The even array is:\n");
for(int i=0;i<evencount;i++)
{
printf("%d ",even[i]);
}
printf("\nThe odd array is:\n");
for(int i=0;i<oddcount;i++)
{
printf("%d ",odd[i]);
}
return 0;
}
REARRANGEMENT OF ARRAY:
#include<stdio.h>
int main(){
int a[5]={0,1,2,3,4};
int b[5]={4,0,1,2,3};
int c[5];
int j=0;
for(int i=1;i<5;i++)
{
c[i]=a[j];
j++;
}
c[0]=a[4];
printf("The new array is :");
{
for(int i=0;i<5;i++)
printf("%d ",c[i]);
}
return 0;
}
CODE TO FIND TWO LARGEST NUMBERS IN A ARRAY:
#include<stdio.h>
int main(){
int a[10];
printf("Enter the 10 elements in array.\nPlease dont enter more than 10 elements it will give you an
error\n");
for(int i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
int temp=a[0];
for(int i=0;i<10;i++)
{
if(temp<a[i])
{
temp=a[i];
}
}
int tamp=0;
for(int i=0;i<10;i++)
{
if(tamp<a[i]&&a[i]!=temp)
{
tamp=a[i];
}
}
printf("The two largest numbers in array are %d and %d",tamp,temp);
return 0;
}
CODE TO FIND LARGEST AND SMALLEST NUMBER IN A
ARRAY:
#include<stdio.h>
int main(){
int a[10];
printf("Enter the 10 elements in array.\nPlease dont enter more than 10 elements it will give you an
error\n");
for(int i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
int largest=a[0];
for(int i=0;i<10;i++)
{
if(largest<a[i])
{
largest=a[i];
}
}
int smallest=a[0];
for(int i=0;i<10;i++)
{
if(smallest>a[i])
{
smallest=a[i];
}
}
printf("The smallest number of array is %d and largest is %d ",smallest,largest);
return 0;
}

CODE TO FIND GREATEST ELEMENT IN ARRAY AND


SMALLEST ELEMENT IN ARRAY AND STORE LARGEST
NUMBER AT 1ST POSITION AND SMALLEST AT LAST
POSITION:
#include<stdio.h>
int main(){
int x;
int largestindex=0;
int smallestindex=0;
printf("Tell how many numbers do you to enter in a array?\n");
scanf("%d",&x);
int a[x];
printf("Now enter the elements please\n");
for(int i=0;i<x;i++)
{
scanf("%d",&a[i]);
}
int largest=a[0];
for(int i=1;i<x;i++)
{
if(largest<a[i])
{
largest=a[i];
largestindex=i;
}
}
int smallest=a[0];
for(int i=0;i<x;i++)
{
if(smallest>a[i])
{
smallest=a[i];
smallestindex=i;
}
}
printf("The largest element in array is %d\n",largest);
printf("The smallest element in array is %d\n",smallest);
int temp=a[0];
a[0]=largest;
a[largestindex]=temp;

int tamp;
tamp=a[x-1];
a[x-1]=smallest;
a[smallestindex]=tamp;
printf("The array after swapping is :\n");
for(int i=0;i<x;i++)
{
printf("%d ",a[i]);
}
return 0;
}

CODE TO SWAP TWO ARRAYS:


#include<stdio.h>
int main(){
int a[3];
int b[3];
printf("Enter the elements in array A\n");
for(int i=0;i<3;i++)
{
scanf("%d",&a[i]);
}
printf("Now enter the elements in array B\n");
for(int i=0;i<3;i++)
{
scanf("%d",&b[i]);
}
int c[3];
for(int i=0;i<3;i++)
{
c[i]=b[i];
}
for(int i=0;i<3;i++)
{
b[i]=a[i];
}
for(int i=0;i<3;i++)
{
a[i]=c[i];
}
printf("Now the new array A is:\n");
for(int i=0;i<3;i++){
printf("%d ",a[i]);}
printf("\nNow the new array B is:\n");
for(int i=0;i<3;i++)
{
printf("%d ",b[i]);
}
return 0;
}

CODE TO PERFORM N number OF RIGHT AND LEFT SHIFTS


ON ANY ARRAY OF ANY NUMBER:
#include <stdio.h>

int main() {
int x;
printf("How many elements do you want to enter in array\n");
scanf("%d",&x);
int a[x];
printf("Now enter the elements\n");
for(int i=0;i<x;i++)
{
scanf("%d",&a[i]);
}
int direction;
printf("In which direction do you want the shift ?\nEnter 1 for left shifts and 0 for right shifts\n");
scanf("%d",&direction);
if(direction==1)
{
int shifts;
printf("How many shifts do you want to perform\n");
scanf("%d",&shifts);
shifts=shifts%x;
while(shifts>0)
{
int temp=a[0];
for(int i=1;i<x;i++)
{
a[i-1]=a[i];
}
a[x-1]=temp;
shifts--;
}
}
else if(direction==0)
{
int shifts;
printf("How many shifts do you want to perform\n");
scanf("%d",&shifts);
shifts=shifts%x;
while(shifts>0)
{
int temp=a[x-1];
for(int i=x;i>0;i--)
{
a[i]=a[i-1];
}
a[0]=temp;
shifts--;
}
}
printf("So array after the desired number of shifts is is :\n");
for(int i=0;i<x;i++)
printf("%d ",a[i]);
return 0;
}

You might also like