Wednesday, September 17, 2014

program that asks the user to input n number of records that he/she wants to enter by using malloc function, then display the user input values

0 comments

Problem

Student structure contains the following contents:
Roll number (int), Name (string), Department (string), Course (string), Year of joining (string), cgpa (float)
Write a program that first prompts the user to input the n number of student records that he/she wants to input in that program. Then use malloc function to dynamically allocate memory that has n number of elements (type of each element is struct student). Then input records from user and display them on screen.
Hint:  p = (struct student*) malloc(sizeof (struct student) * n);

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
 char name[30];
 int roll_no;
 char department[30];
 char course[30];
 int year;
 float cgpa;
};
main()
{
 struct student *s;
 int i,how_many;

 printf ("Number of student data you want to enter");
 scanf ("%d",&how_many);

 s = (struct student*) malloc(sizeof(struct student) * how_many);

 for (i=0;i<how_many;i++)
 {
  fflush(stdin);
  printf ("enter name: ");
  gets(&s[i].name);

  printf ("Enter Roll no: ");
  scanf ("%d",&s[i].roll_no);

  fflush(stdin);
  printf ("enter department: ");
  gets (&s[i].department);

  fflush (stdin);
  printf ("enter course: ");
  gets (&s[i].course);

  printf ("enter year: ");
  scanf ("%d",&s[i].year);

  printf ("enter cgpa: ");
  scanf ("%f",&s[i].cgpa);
 }

 printf("\nAnd here is whhat you have entered\n\n");
 for (i=0;i<how_many;i++)
  printf("Name = %s\n\nRoll No. = %d\n\nDepartment = %s\n\nCopurse = %s\n\nYear = %d\n\n",s[i].name,s[i].roll_no,s[i].department,s[i].course,s[i].year);

 system("pause");
}

Sample Output


using malloc function

Read More..

Sunday, September 14, 2014

Display the user input names by allocating memory dynamically (malloc function)

0 comments

Problem

Write a program that asks the user to input name of students and then displays these names on computer screen. You have to write a program such that it should work with any numbers of names. e.g. users can input 6 names or 100 names.
  1. For such problems declare an array of pointers to characters.
  2. Input a name from user in a temp array of size 50 using gets( ) function. Then allocate a dynamic memory to store that name and assign the base address of dynamic memory to the first index of array of pointers.   
  3. Do the processes in b again and again until the user input an empty string.
  4.  Then displays all the names using array of pointers

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include <string.h>
int main ()
{
 char *name[30];
 char temp[30];
 int i, k;

 printf ("Enter names: ");

 for (i=0;i<1000;i++)
 {
  fflush(stdin);

  gets (temp);

  if (strlen(temp) != 0)
  {
   name[i] = (char *) malloc (strlen(temp)+1);
   strcpy (name[i],temp);
  }
  else
   break;
 }

 printf ("\n\nNames that you have entered are: ");

 for (k=0;k<i;k++)
 {
  printf ("\n%s",name[k]);
 }

 printf ("\n\npress any key to close.");
 getch();
}

Sample Output

dynamic memory allocating using malloc function

Read More..

write a program to calculate sum and average by allocating dynamic memory (malloc function)

0 comments

Problem

Write a program that asks the user to input marks of some subjects. The program calculates sum and average of those marks and prints them on screen. As a programmer you are not sure that a user wants to input marks of how many subjects. So, your program first asks the user for how many subject you want to input marks. The program allocates dynamic memory using malloc function.
Prototype of malloc function is:
void* malloc (int n);

where n is the number of bytes to be allocated you have to type cast void* pointer to int* since you know that you will be storing int values in that allocated memory.

Note: Use free function when the program ends.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include <string.h>
int main ()
{
 int sum=0, avg, i;
 int how_many;
 int *p;

 printf ("Enter number of subjects whose marks you want to input: ");
 scanf ("%d",&how_many);

 p = (int *)malloc(how_many * 4);

 if (p == NULL)
 {
  printf(" Out of memory!\n");
  printf ("Press any key to close.");
  exit(0);
 }

 for (i=0;i<how_many;i++)
 {
  printf ("Enter marks of %dth subject: ",i+1);
  scanf ("%d",&p[i]);

  sum = (sum + p[i]);
 }

 avg = (sum / how_many);

 printf ("Sum = %d\n\n",sum);
 printf ("Average = %d\n\n",avg);

 free (p);

 printf ("press any key to close.");
 getch();
}

Sample Output


calculate sum & average using malloc function

Read More..

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact