C: Pointers and eigenfunctions - Applications resolved


1) Example in declaring and using pointer variables. Transfer is made between addresses of memory locations, with simultaneous retention and retained values to them.
#include<stdio.h>
void main()
{
int a=1;	/* "a" is a memory location to two elements of type int
				on bytes, we initialized it with value 1 */
 
int* b;     	/*"b" is a variable pointer to type int, which is capable of
				address retain other variables of the specified type */
		//declare pointer

b=&a;		/*to address "b" retain memory address location (ie,
				variable) "a", then "b" is the address of memory location "a" */
	
	printf("Value a= %d\t Value b= %d\n\n", a,*b);
	printf("Adresse a= %p\t Adresse b= %p", &a, b);
	
	getchar();
	int k;
	scanf("&d", k);
}
2) Example in declaring and using pointer variables. Transfer will only enter values retained in certain memory locations.
#include<stdio.h>
void main()
{
	int a=1;	/*"a" is a memory location to two elements of type int
			on bytes, we initialized it with value 1  */
 
	int x=55;
 
	int *b;		/* "b" is a variable pointer to type int, which is capable of
			address retain other variables of the specified type */
			//declare pointer

	
	b=&x;		//initialize pointer
			/* to address "b" retain memory address location (ie,
			variable) "x", then "b" is the address of memory location "x"  */
 
	*b = a;		/* transfer is made between values;
			retained value at b assign the value retained in a*/
	
	printf("Value a= %d\t Value b= %d\n\n", a,*b);
	printf("Address a= %p\t Address b= %p", &a, b);
 
	getchar();
	int k;
	scanf("&d", k);
}
3) Transmission of information through addresses. Interchange will be between addresses of memory locations,interchange is also an auxiliary element address.
#include<stdio.h>
void main()
{
	int *a, *b, *c;
	int x=55;
	int y=66;
 
	a=&x;	//initialize a
	b=&y;	//initialize b
	
	printf("Enter a: ",a);
	scanf("%d", a);
	printf("Enter b: ",b);
	scanf("%d", b);
 
	printf("\nBefore merge: \n");
	
	printf("\tValue a= %d\tValue b= %d\n\n", *a,*b);
	printf("\tAddress a= %p\tAddress b= %p\n", a, b);
 
	//merge
	c=a;
	a=b;
	b=c;
 
	printf("\nAfter merge: \n");
	
	printf("\tValue a= %d\tValue b= %d\n\n", *a,*b);
	printf("\tAddress a= %p\tAddress b= %p\n", a, b);
 
	getchar();
	int k;
	scanf("&d", k);
}
4) Transmission of information through addresses. Interchange will be between addresses of memory locations, interchange is not an auxiliary element address (a pointer).
#include<stdio.h>
void main()
{
	int *a, *b, c;
	int x=2, y=5;
 
	a=&x;	//initialize a
	b=&y;	//initialize b

	printf("Enter a: ",a);
	scanf("%d", a);
	printf("Enter b: ",b);
	scanf("%d", b);
 
	printf("\nBefore merge: \n");
	
	printf("\tValue  a= %d\tValue  b= %d\n\n", *a,*b);
	printf("\tAddress a= %p\tAddress b= %p\n", a, b);
	
	//merge

	c=*a;	// transfer between values, for the expression "& c = a;" was not valid
	a=b;
	b=&c;
 
	printf("\nAfter merge: \n");
	
	printf("\tValue  a= %d\tValue  b= %d\n\n", *a,*b);
	printf("\tAddress a= %p\tAddress b= %p\n", a, b);
 
	getchar();
	int k;
	scanf("&d", k);
}
5) Transmission of information through addresses. Interchange is made between the values retained in memory locations of those addresses, auxiliary element interchange is an address (a pointer).
#include<stdio.h> 
void main()
{
	int *a, *b, *c;
	int x=2, y=5, z=7;
 
	a=&x;	//initialize a
	b=&y;	//initialize b
	c=&z;	//initialize c

	printf("Enter a: ",a);
	scanf("%d", a);
	printf("Enter b: ",b);
	scanf("%d", b);
 
	printf("\nBefore merge: \n");
	
	printf("\tValue  a= %d\tValue  b= %d\n\n", *a,*b);
	printf("\tAddress a= %p\tAddress b= %p\n", a, b);
	
	//merge

	*c=*a;	// transfer between values
	*a=*b;
	*b=*c;
 
	printf("\nAfter merge: \n");
	
	printf("\tValue  a= %d\tValue  b= %d\n\n", *a,*b);
	printf("\tAddress a= %p\tAddress b= %p\n", a, b);
 
	getchar();
	int k;
	scanf("&d", k);
}
6) Transmission informations through adress. Interchange is made between the values retained in memory locations of these addresses, interchange is not an auxiliary element address (a pointer).
#include<stdio.h>
void main()
{
	int *a, *b, c;
	int x=2, y=5;
 
	a=&x;	//initialize a
	b=&y;	//initialize b
	
 
	printf("Enter a: ",a);
	scanf("%d", a);
	printf("Enter b: ",b);
	scanf("%d", b);
 
	printf("\nBefore merge: \n");
	
	printf("\tValue a= %d\tValue b= %d\n\n", *a,*b);
	printf("\tAddress a= %p\tAddress b= %p\n", a, b);
	
	//merge

	c=*a;		// transfer between values
	*a=*b;
	*b=c;
 
	printf("\nAfter merge: \n");
	
	printf("\tValue a= %d\tValue b= %d\n\n", *a,*b);
	printf("\tAddress a= %p\tAddress b= %p\n", a, b);
 
	getchar();
	int k;
	scanf("&d", k);
}
7) Pointer arithmetic. Write a routine in C for exemplification in case of the arithmetic pointer, exemplification on operations of increment / decrement, or assembly / decrease values.
#include<stdio.h>
void main()
{
 
	int a[]={10,20,30,40,50};	/*declared a string  elements of type int which initialized 
								with values that are in crowd*/
	int* b;				// b is pointer to type int
	b=a;			        //in address b retain string a
/* transfer make so because a is one address  by his name, address first element of string*/
 
	for(int i=1; i<=5; i++)
	{
	printf("%d\n", *b);		/* b by transfer was address first element, result of this expression, 
					will be first value of string, ie 10*/
	b++;				//switching to the next item in string
	}
 
	// we will return with the cursor pointer of the string in first position
		b-=5;	//or b=b-5
		printf("The first element the string: %d\n", *b);
 
	/* maintain our point position in the string, if you want to display the third element of the string */
	printf("The third element the string: %d\n", *(b+2));
 
 
	getchar();
	int k;
	scanf("&d", k);
}
8) Pointer arithmetic. Write a routine in C for exemplification in case of the arithmetic pointer, exemplification on operations of increment / decrement, or assembly / decrease on a string in its substrings processing on.
#include<stdio.h>
void main()
{
	char a[]="ABCDE"; ///string a retain a string of character, in number of 5
	char* b;
	b=a;
 
	for(int i=1;i<=5;i++)
	{
		printf("%s\n",b); /* a string is one address, and the message will display 
					first instring starting position, ie "ABCDE" */
		b++;
	}
 
	b-=5;
	printf("Substring starting with the first position: %s\n",b);
	printf("Substring starting from the third position: %s",b+2);
 
 
	getchar();
	int k;
	scanf("&d", k);
}
9) Dynamic memory allocation work with pointer variables. To achieve a program that interchange two real values ​​using the pointer variables.
#include<stdio.h>
#include<stdlib.h> //or #include<alloc.h>
void main()
	{
	double *a,*b,*c;
 
	/* We create dynamic variables, ie variables that will keep
		extended memory in an area called the HEAP */
 
	a=(double*)malloc(sizeof(double));	/* HEAP area we reserved space for
						store a variable of type double */
	printf("Enter a: "); scanf("%lf",a);
 
	b=(double*)malloc(sizeof(double));
	printf("Enter b: "); scanf("%lf",b);
	c=a;					/* for "c" is not required to allocate dynamic memory,
						because "c" receives transfer from "a"*/
	a=b;
	b=c;
	printf("after merge: a= %.2lf\tb= %.2lf",*a,*b);
 
	/* although in this case it is not necessary for a dynamic variable
	no longer viable at the end of the function execution,Yet we can choose 
	for explicit deallocation for the variables created dynamic */
 
	free(a);
	free(b);
 
	getchar();
	int k;
	scanf("&d", k);
}
10) Transfer parameters by value.
#include<stdio.h>
void change(int x, int y, int z)
{
	printf("Initial values in function: %d %d %d\n",x,y,z);	//1 2 3
	x+=10;
	y+=10;
	z+=10;
	printf("Final values in function: %d %d %d\n",x,y,z); //11 12 13
}
 
void main()
{
	int a=1,b=2,c=3;
	printf("Initial values in program: %d %d %d\n",a,b,c);	//1 2 3
	change(a,b,c);
	printf("Final values in program: %d %d %d\n",a,b,c); //1 2 3

	getchar();
	int k;
	scanf("&d", k);
}
11) Transfer of parameters by value.
#include<stdio.h>
int sum (int s)
{
	for(int i=0; i<=10; i++)
	s=s+i;
	return s;
}
 
void main()
{
	int s=0;
	sum(s);
	printf("Sum is: %d", s);	//Sum is 0

	getchar();
	int k;
	scanf("%d",k);
}
12) Transfer parameters by address.
#include<stdio.h>
void modifica(int* x, int* y, int* z)
{
	printf("Initial values in function: %d %d %d\n",*x,*y,*z);	//1 2 3
	*x+=10;
	*y+=10;
	*z+=10;
	printf("Final values in function: %d %d %d\n",*x,*y,*z);	//11 12 13
}
void main()
{
	int a=1,b=2,c=3;
	printf("Initial values in program: %d %d %d\n",a,b,c);		//1 2 3  modify(&a,&b,&c);
	
	printf("Final values in program: %d %d %d\n",a,b,c);		//11 12 13

	getchar();
	int k;
	scanf("%d",k);
}
13) Transfer parameters by address.
#include<stdio.h>
void sum(int* s)
{
	for(int i=0; i<=10; i++)
		*s=*s+i;
}
void main()
{
	int s=0;
	sum(&s);
	printf("Sum is: %d\n", s);		//Sum is 55 

 
	getchar();
	int k;
	scanf("%d",k);
}
14) Write a program that will do interchange the contents of two variables which can hold the real value. The program will define functions to read and display a real number, and a function to interchange content variables, the result remains outside the function execution.
#include<stdio.h>
//reading a real variable
void read(float* x, char c)
{
	printf("Enter %c: ", c); 
	scanf("%f", x);
} 
//display the contents of a real variable
void display(float x, char c)
{
	printf("Value from %c is: %.2f\n", c,x); 
} 
//merge function the contents of two real variables
void merge(float *x, float *y)
{ 
	float z;
	z=*x;
	*x=*y;
	*y=z;
} 
void main()
{
	float a, b;
	read(&a,'a');
	read(&b,'b');
 
	printf("\nINITIAL:\n");
	display(a,'a');
	display(b,'b');
 
	printf("\nAFTER MERGE:\n");
	merge(&a,&b);
	display(a,'a');
	display(b,'b');
 
	getchar();
	int k;
	scanf("%d",k);
}
15) Write a program that will do interchange the contents of two variables which can hold the real value. The program will define functions to read and display a real number, and a function to interchange content variables, the result remains outside the function execution. Global variables are declared; declared by a type defined standard.
#include<stdio.h>
float a,b;
//reading function of two variables
void read()
{
	printf("Enter a: "); 
	scanf("%f", &a);
	printf("Enter b: "); 
	scanf("%f", &b);
}
//display function
void display()
{
	printf("Values are: a=%.2f, b=%.2f\n", a, b); 
}
//merge function
void merge()
{ 
	float c;
	c=a;
	a=b;
	b=c;
}
void main()
{	
	read(); 
	display(); 
	printf("\n"); 
	merge(); 
	display(); 
	getchar();
	int k;
	scanf("%d",k);
}
16) Write a program that will make content interchange of two variables which can retain actual values. The program will define functions for reading and display one real number and an own function for interchange the contents of variables with staying execution result and outside work. Global variables are declared, declared a pointer to one real type.
#include<stdio.h>
#include<stdlib.h>
float*a,*b;
void allocation()
{
	a=(float*)malloc(sizeof(float));
	b=(float*)malloc(sizeof(float));
}
void read()
{
	allocation();
	printf("Enter a: ");
	scanf("%f",a);
	printf("Enter b: ");
	scanf("%f",b);
}
void display()
{
	printf("Values are: a=%.2f, b=%.2f\n",*a,*b);
}
void merge()
{
	float c;
	c=*a;
	*a=*b;
	*b=c;
}
void main()
{
	read();
	display();
	printf("\n");
	merge();
	display();
 
	getchar();
	int k;
	scanf("%d");
}
17) What will display the program below?
#include<stdio.h>
 
int a,b;
int f(int m,int n)
{
	m=n+b;
//printf("\nm=%d", m);

	n+=1;
//printf("\nn=%d", n);

	return n+b+m;
//printf("\nm=%d", n+b+m);
}
void main()
{
	a=6;
	b=5;
printf("\n%d %d ",a,b);
printf("\n%d ",f(a,b));
printf("\n%d %d ",a,b);
 
	getchar();
	int k;
	scanf("%d",k);
}
 
Response: 6 5 21 6 5 
18) Write a function that will calculate the sum of the digits of a number. Function prototype will be written (declarative differentiated from the final area).
#include<stdio.h>
//in this case the result will be sent by result type
int sum(int); //sum of the digits of an integer

void main()
{
	int n, sum_digits=0, no_inverse=0;
	printf("Enter n: ");
	scanf("%d",&n);
 
	sum_digits=sum(n);
	printf("\nSum of its digits is: %d", sum_digits);
 
	getchar();
	int k;
	scanf("%d",k);
}
 
/* sum of the digits of an integer:
Suppose no. wanted to be 523 to calculate the sum digits 
523 : 10 = 52 remnant 3
Intermediate sum digits: 0+3=3
52 : 10 = 5 remnant 2
Intermediate sum digits: 3+2=5
5 : 10 = 0 remnant 5
Final sum digits: 5+5=10
*/
 
int sum(int n)
{
	int s=0;
	while (n)
		{
			s=s+n%10;
			n/=10;
		}
	return s;
}
/* inverse of an integer:
Suppose no. 523 desired as we want to find the inverse
523 : 10 = 52 remnant 3
Inverse intermediate number: 0*10+3=3
52 : 10 = 5 remnant 2
Inverse intermediate number: 3*10+2=32
5 : 10 = 0 remnant 5
Inverse final number: 32*10+5=325
*/
19) Using a function make a program that calculates the following expression: (1+ ½ + 1/3+ ¼ +…+1/n)^n where n is a given integer from keyboard.
#include<stdio.h>
#include<math.h>

float sum (int n)
{
float s=0;
float p=1;
for(int i=1; i<=n; i++)
	{
		p=1/(float)i;
		s=s+p;
	}
s=pow(s,n); 
return s;
}
void main()
{
	int n;
 
	printf("\nEnter n: ");
	scanf("%d", &n);
	printf("\n\tThe result of the expression is: %.2f", sum(n));
 
	getchar();
	int k;
	scanf("%d", k);
}
20) Write a program that calculates the gcd and lcm through two function of two numbers from the keyboard.
#include <stdio.h>
#include <math.h>

int gcd (int n, int m)
{
 while(n!=m)
	{ 
		if(n>m)
			n=n-m;
		else
			m=m-n;
	}
return n;
}
 
int lcd (int n, int m)
{
	int z;
	z=(n*m)/gcd(n,m);
	return z;
}
 
void main()
{
	int n,m;
	printf("\nEnter n: ");
	scanf("%d", &n);
 
	printf("\nEnter m: ");
	scanf("%d", &m);
 
printf("\ngcd is: %d", gcd(n,m));
printf("\nlcd is: %d", lcd(n,m));
 
getchar();
int k;
scanf("%d", k);
}
21) Realize addition and multiplication of two real numbers using pointer type variables.
#include<stdio.h>
void main()
{
	int a;
	printf("Enter a: ");
	scanf("%d", &a);
 
	int b;
	printf("Enter b: ");
	scanf("%d", &b);
 
	int* n; //declare pointer
	int* m;	//declare pointer
	
	n=&a;	//initialize pointer
	m=&b;	//initialize pointer

 
	printf("\nSum is: %d", *n+*m);
	printf("\nProduct is: %d", *n * *m);
 
	getchar();
	int k;
	scanf("%d", k);
}
22) Write a function that receives as a parameter an edge length of a square and returns its area and a function that returns the diagonal.
#include<stdio.h>
#include<math.h>
float length (int n)
{
	float area;
	area=n*n;
	return area;
}
 
float diagonal (double n)
{
	double diag;
	diag=sqrt((n*n)+(n*n));	
	return diag;
}
 
void main()
{
int n;
printf("Enter n: ");
scanf("%d", &n);
 
printf("\nSquare area is: %.2f", length(n));
printf("\nDiagonal square is: %.2lf", diagonal(n));
 
getchar();
int k;
scanf("%d", k);
}
23) Write a function that receives three parameters of type real, with the significance of lengths for 3 segments. Function returns 1 if the three segments can form a triangle and 0 otherwise.
#include<stdio.h>
#include<math.h>
int triangle(float x, float y, float z)
{
	if((x>0)&&(y>0)&&(z>0)&&(x<y+z)&&(y<x+z)&&(z<x+y))
	return 1;
		else return 0;
}
void main()
{
	float x;
	float y;
	float z;
	printf("Enter x: ");
	scanf("%f", &x);
	printf("Enter y: ");
	scanf("%f", &y);
	printf("Enter z: ");
	scanf("%f", &z);
 
	printf("Result: %d",triangle(x,y,z)),
 
	getchar();
	int k;
	scanf("%d", k);
}
24) Write a function that returns the last digit of natural number. For example, if number is 234, function will return 4.
#include<stdio.h>
#include<math.h>
int digit (int a)
{
	int b;
	b=a%10; 
	return b;
} 
void main()
{
	int a;
	
	printf("Enter the number: ");
	scanf("%d", &a);
 
	printf("The last digit is: %d",digit(a));
 
getchar();
int k;
scanf("%d", k);
}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.