Friday, March 10, 2017

find index number of the largest element using class

0 comments

Problem

Start with a program that allows the user to input a number of integers, and then stores them in an int array. Write a function called maxint() that goes through the array, element by element, looking for the largest one. The function should take as arguments the address of the array and the number of elements in it, and return the index number of the largest element. The program should call this function and then display the largest element and its index number.

Code

// p[4].cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<conio.h>
#include<iostream>
#include<string.h>
using namespace std;

class intgrs
{
private:
int a[5];
int i,j,z;

public:
intgrs():j(0),i(0),z(0)
{ }

void maxint()
{
cout<<"Enter Integers: ";
for(i=0; i<5; i++)
{
cin>>a[i];
}
for(i=0;i<5;i++)
{
if(a[i]>j)
{
j = a[i];
z=i;
}
}
cout<<"\n\nLargest Element = "<<j;
cout<<"\n\nIndex Number = "<<(z+1)<<endl;
}
};

int main()
{
intgrs i;

i.maxint();

cout<<"\n\nPress any key to close.";
getch();
return 0;
}

Output

find index number of the largest element using class

Read More..

Sunday, September 14, 2014

compare two given dates using structure

2 comments

Problem

Write a program that compares two given dates. To store date use structure say date that contains three members namely date, month, and year. If the dates are equal then display message as "Equal" otherwise "Unequal".

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct date
{
 int date;
 int month;
 int year;
};
int main ()
{
 int i,f=0;  struct date d[2];  for(i=0;i<2;i++)
 {
  printf("\nEnter day for the %dth date\n",i+1);
  scanf("%d",&d[i].date);

  printf("\nEnter the month for the %dth date\n",i+1);
  scanf("%d",&d[i].month);

  printf("\nEnter the year for the %dth date\n",i+1);
  scanf("%d",&d[i].year);
 }
 if(d[0].date==d[1].date)
 {
  if(d[0].month==d[1].month)
  {
   if(d[0].year==d[1].year)
   {
    f=1;
   }
  }
 }
 if(f==1)
  printf("\nThe dates are equal");
 else
  printf("\nThe dates are not equal");

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

Sample Output

compare dates using structure

Read More..

write a program that allows the user to input data and search

0 comments

Problem

Create a structure to specify data on students given below:
Roll number, Name, Department, Course, Year of joining
Create an array of structure. Use a loop to input values and then display those values. Also write a function:
void searchstudent (int roll_no);
This function will search a student record from that array whose Roll number is same as roll_no and display that record otherwise it displays record not found.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void searchstudent (int roll_no);
struct book
{
 int roll;
 char name[20];
 char department[20];
 char course[20];
 int year;
}b[2];
int main ()
{
 int i, roll_no;

 for (i=0;i<2;i++)
 {
  printf ("Enter roll no. : ");
  scanf ("%d",&b[i].roll);

  fflush (stdin);
  printf ("Enter name: ");
  gets (b[i].name);

  fflush (stdin);
  printf ("Enter department: ");
  gets (b[i].department);

  fflush (stdin);
  printf ("Enter course: ");
  gets (b[i].course);

  printf ("Enter year: ");
  scanf ("%d",&b[i].year);
 }

 for (i=0;i<2;i++)
  printf("\n\nRoll number: %d\nName: %s\nDepartment: %s\nCourse: %s\nYear: %d\n",b[i].roll,b[i].name,b[i].department,b[i].course,b[i].year);

 printf ("SEARCH DATA(enter roll number): ");
 scanf ("%d",&roll_no);

 searchstudent(roll_no);

 printf ("\npress any key to close.");
 getch();
}
void searchstudent (int roll_no)
{
 int i;

 for (i=0;i<2;i++)
 {
  if (roll_no == b[i].roll)
   printf("\n\nRoll number: %d\nName: %s\nDepartment: %s\nCourse: %s\nYear: %d\n",b[i].roll,b[i].name,b[i].department,b[i].course,b[i].year);
 }
}

Sample Output

search of a student using structure

Read More..

create an array of structure to display user input values

0 comments

Problem

Create a structure to specify data on students given below:
  • Roll number
  • Name
  • Department
  • Course
  • Year of joining
Create an array of that structure. Use a loop to input values and then display those values.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct book
{
int roll;
char name[20];
char department[20];
char course[20];
int year;
}b[2];
int main ()
{
 int i;

 for (i=0;i<2;i++)
 {
  printf ("Enter roll no. : ");
  scanf ("%d",&b[i].roll);

  fflush (stdin);
  printf ("Enter name: ");
  gets (b[i].name);

  fflush (stdin);
  printf ("Enter department: ");
  gets (b[i].department);

  fflush (stdin);
  printf ("Enter course: ");
  gets (b[i].course);

  printf ("Enter year: ");
  scanf ("%d",&b[i].year);
 }

 for (i=0;i<2;i++)
  printf("\n\nRoll number: %d\nName: %s\nDepartment: %s\nCourse: %s\nYear: %d\n",b[i].roll,b[i].name,b[i].department,b[i].course,b[i].year);

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

Sample Output

using array of structure to display user input data

Read More..

Tuesday, September 9, 2014

Library management using structure

8 comments

Problem

Write a menu driven program that depicts the working of a library. The menu options should be:
  1. Add book information
  2. Display book information
  3. List all books of given author
  4. List the title of specified book
  5. List the count of books in the library
  6. List the books in the order of accession number
  7. Exit
Create a structure called library to hold accession number, title of the book, author name, price of the book, and flag indicating whether book is issued or not.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct library
{
 char bk_name[30];
 char arthur[30];
 int pages;
 float price;
};

int main()
{
 struct library l[3];
 char ar_nm,bk_nm;
 int i,j;

 printf ("1. Add book information\n2. Display book information\n3. List all books of given author\n4. List the title of specified book\n5. List the count of books in the library\n6. Exit");

 printf ("\n\nEnter one of the above : ");
 scanf("%d",&j);

 switch (j)
 {
  case 1:
   for (i=0;i<3;i++)
   {

    printf ("Enter book name = ");
    scanf ("%s",&l[i].bk_name);

    printf ("Enter arthur name = ");
    scanf ("%s",&l[i].arthur);

    printf ("Enter pages = ");
    scanf ("%d",&l[i].pages);

    printf ("Enter price = ");
    scanf ("%f",&l[i].price);
   }
   break;
  case 2:
   printf("you have entered the following information\n");
   for(i=0;i<3;i++)
   {
    printf ("book name = %s",l[i].bk_name);

    printf ("arthur name = %s",l[i].arthur);

    printf ("pages = %d",l[i].pages);

    printf ("price = %f",l[i].price);
   }
   break;

  case 3:
   printf ("Enter arthur name : ");
   scanf ("%s",&ar_nm);
   for (i=0;i<3;i++)
   {
    if (ar_nm == l[i].arthur)
     printf ("%s %s %d %f",l[i].bk_name,l[i].arthur,l[i].pages,l[i].price);
   }
   break;

  case 4:
   printf ("Enter book name : ");
   scanf ("%s",&bk_nm);
   for (i=0;i<3;i++)
   {
    if (bk_nm == l[i].bk_name)
     printf ("%s %s %d %f",l[i].bk_name,l[i].arthur,l[i].pages,l[i].price);
   }
   break;

  case 5:
   for (i=0;i<3;i++)
   {

   }
   break;
  case 6:
   exit (0);


 }
 return 0;
}

Sample Output

library working using struct

Read More..

compare two dates input by the user, using structure

0 comments

Problem

Write a program that compares two given dates. To store date use structure say date that contains three members namely date, month and year. If the dates are equal then display message as "Equal" otherwise "Unequal".

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct dt_cmp
{
 int day;
 int month;
 int year;
};
main()
{
 struct dt_cmp s[2];
 int i;

 for (i=0;i<2;i++)
 {
  printf ("Enter day : ");
  scanf ("%d",&s[i].day);
  printf ("Enter month : ");
  scanf ("%d",&s[i].month);
  printf ("Enter year : ");
  scanf ("%d",&s[i].year);
 }

 if ( (s[0].day == s[1].day) && (s[0].month == s[1].month) && (s[0].year == s[1].year) )
  printf ("Equal\n");
 else
  printf ("Unequal\n");

 system("pause");
}

Sample Output

compare dates using structres

Read More..

search for a user (student) input data by using an array of structure, add user roll number, name, department, course, and year of joining by declaring a function name searchstudent(int)

0 comments

Problem

Create a structure to specify data on students given below:
  • Roll number
  • Name
  • Department
  • Course
  • Year of joining
Create above program by declaring an array of structure., input some value from user and then display those values. Also write a function:
void searchstudent (int roll_no);
This function will search a student record from that array whose Roll number is same as roll_no and display that record otherwise it displays record not found.

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;
};
void searchstudent (struct student *, int );

int main()
{

 struct student s[5];
 int i,rl,p;

 for (i=0;i<5;i++)
 {
  printf ("Enter name, ID, department, course, year : ");
  scanf("%s%d%s%s%d",&s[i].name,&s[i].roll_no,&s[i].department,&s[i].course,&s[i].year);
 }

 for (i=0;i<5;i++)
 {
  printf("And here is whhat you have entered\n\n");
  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);
 }

 printf ("Enter number you want to search : ");
 scanf("%d",&rl);

 searchstudent (s ,rl);

 system("pause");
}
void searchstudent (struct student *p, int roll_no)
{
 int i,found = 0;
 for(i=0;i<5;i++)
 {
  if(p->roll_no == roll_no)
  {
   printf("record found");
   printf("Name = %s\n\nRoll No. = %d\n\nDepartment = %s\n\nCopurse = %s\n\nYear = %d\n\n",p->name,p->roll_no,p->department,p->course,p->year);
   found = 1;
  }
  if (found == 0)
   printf("Not found");
 }

}

Sample Output

search user roll# name using array of structure

Read More..

Display user input data by using an array of structure, add user roll number, name, department, course, and year of joining

0 comments

Problem

Create a structure to specify data on students given below:
  • Roll number
  • Name
  • Department
  • Course
  • Year of joining
Create above program by declaring an array of structure., input some value from user and then display those values.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct stud
{
 char name[30];
 int roll_no;
 char department[30];
 char course[30];
 int year;
}
int main()
{
 struct stud s[5];
 int i;

 for (i=0;i<5;i++)
 {
  printf ("Enter name, ID, department, course, year : ");
  scanf("%s%d%s%s%d",&s[i].name,&s[i].roll_no,&s[i].department,&s[i].course,&s[i].year);
}

 for (i=0;i<5;i++)
 {
  printf("And here is whhat you have entered\n\n");
  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");
 return 0;
}

Sample Output

display user roll# name using array of structure

Read More..

Sunday, September 7, 2014

find how many times a character appears in a string

0 comments

Problem

write a c program that will ask the user to input a character and then the program will check whether the character (how many times) exists in the following string or not.

Note: use pointers to find the character in the string character by character.

Solution - Code

#include<stdio.h>
#include<conio.h>
int countchar(char *p, char a);
main()
{
 int count;
 char a;
 char p[]="OMG! its hot.";
 printf("Enter the character : ");
 scanf("%c",&a);
 count = countchar(p,a);
 printf("\n\nCharacter %c occured in the array %d times.\n",a,count);
 system("pause");
}
int countchar(char *p, char a)
{
 int count=0;
 while (*p != '\0')
 {
  if (a == *p)
   count++;
  p++;
 }
 return (count);
}

Sample Output


find character in a string

Read More..

determine the number of characters in a string

0 comments

Problem

write a program in c language that passes array index as an argument to a user defined function.The function then counts the number of characters in that string and displays it to the user.

Note: use pointers to count the string character by character.

Solution - Code

#include<stdio.h>
#include<conio.h>
int mystrlen(char *p);
main()
{
 int count;
 char p[]="Honesty is the best policy.";
 count = mystrlen(p);
 printf("\n\nNumber of characters in the string = %d\n\n",count);
 system("pause");
}
int mystrlen(char *p)
{
 printf ("\n");
 int count = 0;
 while (*p != '\0' )
 {
  printf ("%c",*p);
  p++;
  count++;
 }
 return (count);
}

Sample Output


number of characters in a string

Read More..

print a string character by character in a user defined function where the string is declared in main

0 comments

Problem

write a program in c language that passes array index as an argument to a user defined function and print the string in that function character by character.

Note: use pointers to print the string character by character.

Solution - Code

#include<stdio.h>
#include<conio.h>
void display(char *p, char end);
main()
{
 char p[]="We are gonna be the best programmers ever.";
 char end= '\0';
 display(p,end);
 printf ("\n\n",*p);
 system("pause");
}
void display(char *p, char end)
{
 printf ("\n",*p);
 while (*p != end)
 {
  printf ("%c",*p);
  p++;
 }
}

Sample Output


display string character by character

Read More..

print a string in a user defined function, where string is declared in main function

0 comments

Problem

write a program in c language that passes array index as and argument to a user defined function and print the string in that function.

Solution - Code

#include<stdio.h>
#include<conio.h>
void display(char *p);
main()
{
 char p[]="Mr.Maaz Ali is a good boy.";
 display(p);
 system("pause");
}
void display (char *p)
{
 printf("\n%s\n\n",p);
}

Sample Output

print string in a function, defined in main

Read More..

Friday, September 5, 2014

check if the number is in the array or not

0 comments

Problem

Write a program that asks the user to input 10 integers and stored them in an array and then again asks user to input any single number that store in variable “Num” and program check whether the Num is in the array or not and then print "Number “Num” is in the array" or “Num” is not in the array".

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
 int arr[10];
 int i,num;
 for (i=0;i<10;i++)
 {
  printf ("Enter number : ");
  scanf ("%d",&arr[i]);
 }
 printf("\nEnter number to check: ");
 scanf ("%d",&num);
 for (i=0;i<10;i++)
 {
  if (num==arr[i])
  {
   printf ("Entered number is in the array\n");
   break;
  }   if (i == 9)
   printf ("Entered number is not in the array\n");
 }
 system("pause");
}

Sample Output

check if number exists in an array

Read More..

display all elements of an array, use pointers to find maximum and minimum element in an array.

0 comments

Problem

Write three user define functions:
  1. void display(int *p, int size);
    The first parameter is the base address of an array and the second parameter is the size of array
    (No of elements in an array). The function displays all the elements in the array. Call this
    function from main function.
  2. int max_value (int *p, size);
    this function returns the maximum element of the array pointed by the pointer p. second
    parameter specify the size of array.
  3. int min_value (int *p, size);
    this function returns the minimum element of the array pointed by the pointer p. second
    parameter specify the size of array.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void display(int *,int);
int max_value (int *,int);
int min_value (int *,int);
main()
{
 int max,min,size,i,x,sum=0,avg;
 int num[5];
 size = 5;
 display(num,size);
 max = max_value (num,size);
 printf ("Maximum element : %d\n",max);
 min = min_value (num,size);
 printf ("Minimum element : %d\n",min);
 system("pause");
}
void display(int *p,int size)
{
 int i,num[size],sum=0,avg;
 for (i=0;i<5;i++)
 {
  printf("Enter numbers: ");
  scanf("%d",&num[i]);
 }
 for (i=0;i<5;i++)
 {
  sum = sum + num [i];
 }
 avg =(sum/5);
 printf ("sum = %d\n",sum);
 printf ("average = %d\n",avg);
}
int max_value (int *p,int size)
{
 int num[size];
 int i,max=num[0];
 for (i=0;i<5;i++)
 {
  printf("Enter numbers: ");
  scanf("%d",&num[i]);
 }
 for (i=0;i<5;i++)
 {
  if (max < num[i])
  max=num[i];
 }
 return max;
}
int min_value (int *p,int size)
{
 int num[size];
 int i,min=num[4];
 for (i=0;i<5;i++)
 {
  printf("Enter numbers: ");
  scanf("%d",&num[i]);
 }
 for (i=5;i>=0;i--)
 {
  if (min > num[i])
  min=num[i];
 }
 return min;
}

Sample Output

find maximum and minimum element c language

Read More..

find sum and average of subjects

0 comments

Problem

Write a program that asks the user to input marks in five different subjects. Then find the sum and average of those marks and print them.
Note: Solve the above problem by using array

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
 int i,x,sum=0,avg;
 int num[5];
 for (i=0;i<5;i++)
 {
  printf("Enter numbers: ");
  scanf("%d",&num[i]);
 }
 for (i=0;i<5;i++)
 {
  sum = sum + num [i];
 }
 avg =(sum/5);
 printf ("sum = %d\n",sum);
 printf ("average = %d\n",avg);
 system("pause");
}

Sample Output

sum and average array c language

Read More..

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