In: Computer Science
PROGRAMMING IN C:
Part 1:
Create a character array and save your first and last name in it
Note: You can assign the name directly or you can use the scanf function.
Display your name on the screen.
Display the address (memory location) in hexadecimal notation of the array. (hint: use %p)
Use a for loop to display each letter of your name on a separate line.
Part 2:
Create a one dimensional array and initialize it with 10 integers of your choice.
Create a function and pass the array elements to that function. The function will calculate the average value of the 10 integers and display the result on the screen.
Part 3:
Create a one dimensional array that contains five integers (10, 20, 30, 40 and 50).
Display the elements of the array on screen
Swap the first element (10) with the last element (50) and then display the elements of the array on the screen (after swapping).
Part 1:
Output:
Code:
#include
int main()
{
char arr[]={"John Peter"};
printf("Name : %s",arr);
printf("\n");
printf("Address : %p",&arr);
int length=sizeof(arr)/sizeof(arr[0]);
printf("\n");
int i;
for(i=0;i
printf("%c",arr[i]);
printf("\n");
}
}
*********************************************************************************************************************
Part 2:
Output:
Code:
#include
void func(int arr[],int length)
{
int sum=0;
int i;
for(i=0;i
sum=sum+arr[i];
}
float average=(float)sum/length;
printf("Average : %f",average);
}
int main()
{
int arr[]={10,25,36,52,15,34,89,12,56,69};
func(arr,10);
}
****************************************************************************************************************************
Part 3:
Output:
Code:
#include
int main()
{
int arr[]={10,20,30,40,50};
printf("\nBefore Swapping :\n\n");
int i;
for(i=0;i<5;i++)
{
printf("%d ",arr[i]);
}
int temp=arr[0];
arr[0]=arr[4];
arr[4]=temp;
printf("\n\nAfter Swapping : \n\n");
for(i=0;i<5;i++)
{
printf("%d ",arr[i]);
}
}