How will you define pointer?
Write a progam that illustrate how pointer variable change the value of
normal variable.
In programming, a pointer is a variable that holds the memory address of another variable. Pointers
are commonly used in programming to access and manipulate data stored in memory.
Here's an example program in C that illustrates how a pointer variable can change the value of a
normal variable:
#include <stdio.h>
#include <conio.h>
int main( ) {
int x = 5; // declare and initialize a normal variable x with value 5
int *p; // declare a pointer variable p that can point to an integer
p = &x; // assign the address of x to pointer p
printf("Value of x before changing through pointer: %d\n", x);
*p = 10; // change the value of x through the pointer p
printf("Value of x after changing through pointer: %d\n", x);
return 0;
}
Q.No. 8 Solution
#include <stdio.h>
#include <graphic.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
int x = 200, y = 200, r = 100;
char text[] = "BCA";
setcolor(YELLOW);
circle(x, y, r);
settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
setcolor(BLUE);
outtextxy(x - textwidth(text)/2, y - textheight(text)/2, text);
getch();
closegraph();
return 0;
}
Q.no. 9 solution
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PLAYERS 20
// Define the structure for each player
struct player {
char name[50];
int runs_scored;
int wickets_taken;
};
// Function to sort players based on wickets taken
void sort_players(struct player* players[], int num_players) {
int i, j;
struct player* temp;
for (i = 0; i < num_players; i++) {
for (j = i+1; j < num_players; j++) {
if (players[i]->wickets_taken < players[j]->wickets_taken) {
temp = players[i];
players[i] = players[j];
players[j] = temp;
}
}
}
}
int main() {
struct player players[MAX_PLAYERS];
struct player* players_ptr[MAX_PLAYERS];
int i;
for (i = 0; i < MAX_PLAYERS; i++) {
printf("Enter details for player %d:\n", i+1);
printf("Name: ");
scanf("%s", players[i].name);
printf("Runs scored: ");
scanf("%d", &players[i].runs_scored);
printf("Wickets taken: ");
scanf("%d", &players[i].wickets_taken);
// Store the pointer to the player in the pointer array
players_ptr[i] = &players[i];
}
// Sort the players based on wickets taken
sort_players(players_ptr, MAX_PLAYERS);
// Print the sorted list of players
printf("\nSorted list of players based on wickets taken:\n");
for (i = 0; i < MAX_PLAYERS; i++) {
printf("%d. %s - %d wickets\n", i+1, players_ptr[i]->name, players_ptr[i]->wickets_taken);
}
return 0;
}
Here's the C code to compute the sum of the series 1+x/1! + x2/2! + x3/3! + …… + xn/N!:
#include <stdio.h>
int main()
{
int n, i;
float x, sum = 1.0, term = 1.0;
printf("Enter the value of x: ");
scanf("%f", &x);
printf("Enter the value of n: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
term *= x / i;
sum += term;
}
printf("Sum of the series = %f", sum);
return 0;
}
/*
* C Program to Find find Sum of the Series 1/1! + 2/2! + 3/3! + ……1/N!
*/
#include <stdio.h>
double sumseries(double);
main()
{
double number,sum;
printf("\n Enter the value: ");
scanf("%lf", &number);
sum = sumseries(number);
printf("\n Sum of the above series = %lf ", sum);
}
double sumseries(double m)
{
double sum2 = 0, f = 1, i;
for (i = 1; i <= m; i++)
{
f = f * i;
sum2 = sum2 +(i / f);
}
return(sum2);
}