This is the platform to get simple 'C' language coding for students who did not afford Tutions...
Friday, 11 November 2016
68.C Program to Store Information Using Structures with Dynamically Memory Allocation
C Program to Store Information Using Structures with Dynamically Memory Allocation
In this example, you'll learn to store information using structures by allocation dynamic memory using malloc().
To understand this example, you should have the knowledge of following C programming topics:
- C Programming Pointers
- C Dynamic Memory Allocation
- C Programming Structure
This program asks user to store the value of noOfRecords and allocates the memory for the noOfRecords structure variable dynamically using malloc() function.
Example: Demonstrate the Dynamic Memory Allocation for Structure
#include <stdio.h>
#include<stdlib.h>
struct course
{
int marks;
char subject[30];
};
int main()
{
struct course *ptr;
int i, noOfRecords;
printf("Enter number of records: ");
scanf("%d", &noOfRecords);
// Allocates the memory for noOfRecords structures with pointer ptr pointing to the base address.
ptr = (struct course*) malloc (noOfRecords * sizeof(struct course));
for(i = 0; i < noOfRecords; ++i)
{
printf("Enter name of the subject and marks respectively:\n");
scanf("%s %d", &(ptr+i)->subject, &(ptr+i)->marks);
}
printf("Displaying Information:\n");
for(i = 0; i < noOfRecords ; ++i)
printf("%s\t%d\n", (ptr+i)->subject, (ptr+i)->marks);
return 0;
}
Output
Enter number of records: 2 Enter name of the subject and marks respectively: Programming 22 Enter name of the subject and marks respectively: Structure 33 Displaying Information: Programming 22 Structure 33
Check out these related examples:
- C Program to Store Information(name, roll and marks) of a Student Using Structure
- C Program to Store Information of Students Using Structure
- C Program to Find Largest Number Using Dynamic Memory Allocation
67.C Program to Store Information of Students Using Structure
C Program to Store Information of Students Using Structure
This program stores the information (name, roll and marks) of 10 students using structures.
To understand this example, you should have the knowledge of following C programming topics:
- C Programming Arrays
- C Programming Structure
In this program, a structure, student is created.
This structure has three members: name (string), roll (integer) and marks (float).
Then, we created a structure array of size 10 to store information of 10 students.
Using for loop, the program takes the information of 10 students from the user and displays it on the screen.
Example: Store Information in Structure and Display it
#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
} s[10];
int main()
{
int i;
printf("Enter information of students:\n");
// storing information
for(i=0; i<10; ++i)
{
s[i].roll = i+1;
printf("\nFor roll number%d,\n",s[i].roll);
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter marks: ");
scanf("%f",&s[i].marks);
printf("\n");
}
printf("Displaying Information:\n\n");
// displaying information
for(i=0; i<10; ++i)
{
printf("\nRoll number: %d\n",i+1);
printf("Name: ");
puts(s[i].name);
printf("Marks: %.1f",s[i].marks);
printf("\n");
}
return 0;
}
Output
Enter information of students: For roll number1, Enter name: Tom Enter marks: 98 For roll number2, Enter name: Jerry Enter marks: 89 . . . Displaying Information: Roll number: 1 Name: Tom Marks: 98 . . .
Check out these related examples:
- C Program to Store Information(name, roll and marks) of a Student Using Structure
- C Program to Store Information Using Structures with Dynamically Memory Allocation
- C Program to Add Two Distances (in inch-feet) System Using Structures
Thursday, 27 October 2016
66.C Program to Calculate Difference Between Two Time Periods
C Program to Calculate Difference Between Two Time Periods
In this example, you'll learn to calculate the difference between two time periods using user defined - differenceBetweenTimePeriod function.
To understand this example, you should have the knowledge of following C programming topics:
- C Programming User-defined functions
- C Programming Structure
- How to pass structure to a function in C programming?
- C Programming Structure and Pointer
Example: Calculate Difference Between Two Time Periods
#include <stdio.h>
struct TIME
{
int seconds;
int minutes;
int hours;
};
void differenceBetweenTimePeriod(struct TIME t1, struct TIME t2, struct TIME *diff);
int main()
{
struct TIME startTime, stopTime, diff;
printf("Enter start time: \n");
printf("Enter hours, minutes and seconds respectively: ");
scanf("%d %d %d", &startTime.hours, &startTime.minutes, &startTime.seconds);
printf("Enter stop time: \n");
printf("Enter hours, minutes and seconds respectively: ");
scanf("%d %d %d", &stopTime.hours, &stopTime.minutes, &stopTime.seconds);
// Calculate the difference between the start and stop time period.
differenceBetweenTimePeriod(startTime, stopTime, &diff);
printf("\nTIME DIFFERENCE: %d:%d:%d - ", startTime.hours, startTime.minutes, startTime.seconds);
printf("%d:%d:%d ", stopTime.hours, stopTime.minutes, stopTime.seconds);
printf("= %d:%d:%d\n", diff.hours, diff.minutes, diff.seconds);
return 0;
}
void differenceBetweenTimePeriod(struct TIME start, struct TIME stop, struct TIME *diff)
{
if(stop.seconds > start.seconds){
--start.minutes;
start.seconds += 60;
}
diff->seconds = start.seconds - stop.seconds;
if(stop.minutes > start.minutes){
--start.hours;
start.minutes += 60;
}
diff->minutes = start.minutes - stop.minutes;
diff->hours = start.hours - stop.hours;
}
OutputEnter start time: Enter hours, minutes and seconds respectively: 12 34 55 Enter stop time: Enter hours, minutes and seconds respectively:8 12 15 TIME DIFFERENCE: 12:34:55 - 8:12:15 = 4:22:40
In this program, user is asked to enter two time periods and these two periods are stored in structure variables startTime and stopTime respectively.
Then, the function
differenceBetweenTimePeriod calculates the difference between the time periods and the result is displayed in main() function without returning it (Using call by reference technique).Check out these related examples:
- C Program to Store Information Using Structures with Dynamically Memory Allocation
- C Program Swap Numbers in Cyclic Order Using Call by Reference
- C Program to Calculate the Power of a Number
65.C Program to Add Two Complex Numbers by Passing Structure to a Function
C Program to Add Two Complex Numbers by Passing Structure to a Function
This program takes two complex numbers as structures and adds them with the use of functions.
To understand this example, you should have the knowledge of following C programming topics:
- C Programming Structure
- How to pass structure to a function in C programming?
Example: Add Two Complex Numbers
#include <stdio.h>
typedef struct complex
{
float real;
float imag;
} complex;
complex add(complex n1,complex n2);
int main()
{
complex n1, n2, temp;
printf("For 1st complex number \n");
printf("Enter real and imaginary part respectively:\n");
scanf("%f %f", &n1.real, &n1.imag);
printf("\nFor 2nd complex number \n");
printf("Enter real and imaginary part respectively:\n");
scanf("%f %f", &n2.real, &n2.imag);
temp = add(n1, n2);
printf("Sum = %.1f + %.1fi", temp.real, temp.imag);
return 0;
}
complex add(complex n1, complex n2)
{
complex temp;
temp.real = n1.real + n2.real;
temp.imag = n1.imag + n2.imag;
return(temp);
}
Output
For 1st complex number Enter real and imaginary part respectively: 2.3 4.5 For 2nd complex number Enter real and imaginary part respectively: 3.4 5 Sum = 5.7 + 9.5i
In this program, structures n1 and n2 are passed as an argument of function
add().
This function computes the sum and returns the structure variable temp to the
main() function.Check out these related examples:
- C program to Find all Roots of a Quadratic equation
- C Program Swap Numbers in Cyclic Order Using Call by Reference
- C Program to Calculate the Power of a Number
64.C Program to Add Two Distances (in inch-feet) System Using Structures
C Program to Add Two Distances (in inch-feet) System Using Structures
This program takes two distances (in inch-feet system), adds them and displays the result on the screen.
To understand this example, you should have the knowledge of following C programming topics:
- C Programming Structure
Example: Program to add two distances in inch-feet system
#include <stdio.h>
struct Distance
{
int feet;
float inch;
} d1, d2, sumOfDistances;
int main()
{
printf("Enter information for 1st distance\n");
printf("Enter feet: ");
scanf("%d", &d1.feet);
printf("Enter inch: ");
scanf("%f", &d1.inch);
printf("\nEnter information for 2nd distance\n");
printf("Enter feet: ");
scanf("%d", &d2.feet);
printf("Enter inch: ");
scanf("%f", &d2.inch);
sumOfDistances.feet = d1.feet+d2.feet;
sumOfDistances.inch = d1.inch+d2.inch;
// If inch is greater than 12, changing it to feet.
if (sumOfDistances.inch>12.0)
{
sumOfDistances.inch = sumOfDistances.inch-12.0;
++sumOfDistances.feet;
}
printf("\nSum of distances = %d\'-%.1f\"",sumOfDistances.feet, sumOfDistances.inch);
return 0;
}
Output
Enter information for 1st distance Enter feet: 23 Enter inch: 8.6 Enter information for 2nd distance Enter feet: 34 Enter inch: 2.4 Sum of distances = 57'-11.0"
In this program, a structure
Distance is defined. The structure has two members inch (a float) and feet (an integer).
Two variables (d1 and d2) are created which stores two distances (in inch and feet). Then, the sum of two distances is stored in sumOfDistances structure and displayed on the screen.
Check out these related examples:
- C Program to Store Information of Students Using Structure
- C Program to Store Information Using Structures with Dynamically Memory Allocation
63.C Program to Store Information(name, roll and marks) of a Student Using Structure
C Program to Store Information(name, roll and marks) of a Student Using Structure
This program stores the information (name, roll and marks) of a student and displays it on the screen using structures.
To understand this example, you should have the knowledge of following C programming topics:
- C Programming Structure
In this program, a structure, student is created.
This structure has three members: name (string), roll (integer) and marks (float).
Then, a structure variable s is created to store information and display it on the screen.
Example: Store Information and Display it Using Structure
#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
} s;
int main()
{
printf("Enter information:\n");
printf("Enter name: ");
scanf("%s", s.name);
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter marks: ");
scanf("%f", &s.marks);
printf("Displaying Information:\n");
printf("Name: ");
puts(s.name);
printf("Roll number: %d\n",s.roll);
printf("Marks: %.1f\n", s.marks);
return 0;
}
Output
Enter information: Enter name: Jack Enter roll number: 23 Enter marks: 34.5 Displaying Information: Name: Jack Roll number: 23 Marks: 34.5
Check out these related examples:
- C Program to Store Information of Students Using Structure
- C Program to Store Information Using Structures with Dynamically Memory Allocation
- C Program to Add Two Complex Numbers by Passing Structure to a Function
Subscribe to:
Posts (Atom)





