0% found this document useful (0 votes)
17 views5 pages

23CHI077

This document contains 4 practical assignments involving pointers and arrays in C programming. The first practical writes a function to add 1 to odd elements and 2 to even elements of a 1D array. The second swaps the values of 3 numbers in a cyclic order. The third prints a 1D array in reverse order using a pointer function. The fourth concatenates two input strings and stores the result in a third string passed to the pointer function. Concepts of pointers, arrays, and functions are used across the practicals.

Uploaded by

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

23CHI077

This document contains 4 practical assignments involving pointers and arrays in C programming. The first practical writes a function to add 1 to odd elements and 2 to even elements of a 1D array. The second swaps the values of 3 numbers in a cyclic order. The third prints a 1D array in reverse order using a pointer function. The fourth concatenates two input strings and stores the result in a third string passed to the pointer function. Concepts of pointers, arrays, and functions are used across the practicals.

Uploaded by

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

Roll No: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]);
}
}

Theoretical concepts: Pointers and arrays and functions


Input and Output:

Practical No: 9-b


Aim :
Write a function that swaps values of three numbers in a cyclic order and prints
the output from main function.
Example: a = 1, b = 2, c = 3 Output: a = 3, b = 1, c = 2
Methodology :
#include<stdio.h>
#include<math.h>
#include<string.h>;
int *function(int *ptr,int n){
int temp = ptr[1];
ptr[1] = ptr[2];
ptr[2] = temp;

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);
}

Theoretical concepts: Pointers and arrays and functions


Input and Output:

CONCLUSIONS
We successfully done practical using pointers.

You might also like