23CHI077
23CHI077
23CHI077
NAME:PAGHADAR PREM
Course Code and Name: 2CS101, Computer Science
Practical No: 9-a
Aim :
Write a UDF using concept of pointers which can accept a one-dimensional array
as an argument. The function should add 1 to all odd element of the array and 2
to all even elements of the array. The final array should be displayed by the
main() function. Repeat this program for two-dimensional array.
Methodology :
#include<stdio.h>
#include<math.h>
#include<string.h>;
int *function(int *ptr,int n){
for(int i = 0; i < n; i++){
if(i % 2 == 0){
ptr[i] += 1;
}
else ptr[i] += 2;
}
return ptr;
}
void main(){
printf("Enter no of element of array\n");
int n = 0;
scanf("%d", &n);
printf("Enter all %d element\n", n);
int arr[n];
for(int i = 0; i < n; i++){
scanf("%d", &arr[i]);
}
int *ptr1 = 0;
ptr1 = function(arr, n);
printf("Modified array is:\n");
for(int i = 0; i < n; i++){
printf("%d ", ptr1[i]);
}
}
temp = ptr[0];
ptr[0] = ptr[1];
ptr[1] = temp;
return ptr;
}
void main(){
printf("Enter any three number\n");
int arr[3];
for(int i = 0; i < 3; i++){
scanf("%d", &arr[i]);
}
int *ptr1 = 0;
ptr1 = function(arr, 3);
printf("Modified array is:\n");
for(int i = 0; i < 3; i++){
printf("%d ", ptr1[i]);
} }
Theoretical concepts: Pointers and arrays and functions
Input and Output:
Practical : 9-c
Aim:
Write a program to print array elements in reverse using pointer.
Methodology :
#include<stdio.h>
#include<math.h>
#include<string.h>;
void function(int *ptr, int n){
printf("Reversed array is:\n");
for(int i = n - 1; i >= 0; i--){
printf("%d ", ptr[i]);
}}
void main(){
printf("Enter no of element of array\n");
int n = 0;
scanf("%d", &n);
printf("Enter all %d element\n", n);
int arr[n];
for(int i = 0; i < n; i++){
scanf("%d", &arr[i]);
}
function(arr, n);
}
Theoretical concepts: Pointers and arrays and functions
Input and Output:
Practical No : 9-d
Aim :
Write a UDF which accepts three strings as arguments. The function should concatenate
first two strings and keep the result in the third string which should be displayed by the
main() function.
Methodology :
#include<stdio.h>
#include<math.h>
#include<string.h>;
char *function(char *ptr1, char *ptr2, char *ptr3){
int k = 0;
for(int i = 0; i < strlen(ptr1); i++){
ptr3[k] = ptr1[i];
k++;
}
for(int i = 0; i < strlen(ptr2); i++){
ptr3[k] = ptr2[i];
k++;
}
return ptr3;
}
void main(){
printf("Enter Two Strings\n");
char a[30], b[30], c[60];
scanf("%s %s", a, b);
char *ptr = 0;
ptr = function(a, b, c);
printf("%s", ptr);
}
CONCLUSIONS
We successfully done practical using pointers.