Close
Close

Sir, can you give me a hint to this problem.I am having problem in passing array of structure to a function and also calling the function.

   pawansaggu

Write a structure to store the roll no., name, age (between 11 to 14) and address of students (more than 10). Store the information of the students.
1 - Write a function to print the names of all the students having age 14.
2 - Write another function to print the names of all the students having even roll no.
3 - Write another function to display the details of the student whose roll no is given (i.e. roll no. entered by the user).

#include <iostream>

using namespace std;
struct student      // defining a structure named student.
{
       string name;
       int roll_no;
       int age;
};

struct student even(struct student a[])            //function for printing student names having even roll number.
                                                                       // Doubt in function variable (struct student a[]). 
{       int i;
        for( i=0;i<5;i++){if(a[i].roll_no%2==0)
        {
         cout<< a[i].name<<endl;
        }
        }
        return a[i];     // I think my return value is incorrect. 
}
struct student age14(struct student a[ ])   //function for printing student names having age 14.
{        int i;
         for( i=0;i<5;i++){if(a[i].age==14)
      {
       cout<< a[i].name<<endl;
      }
      }
      return a[i];
      
}
            
      struct student detail(struct student a[ ])   // Function for getting the detail of a particular student.
{
          int i;
          if(cin>>i)
          {
              cout<< a[i].name<<endl;
              cout<< a[i].roll_no<<endl;
              
          }      
                 return a[i];        
                 
          }
       
       
     


int main(int argc, char *argv[])
{   struct student a[5];
    for(int i=0;i<10;i++)      // loop for taking the input .
    {
            cout<<"Enter the name of the student"<<i+1<<endl;
            cin>>a[i].name;
            cout<<"Enter the roll number"<<endl;
            cin>>a[i].roll_no;
    }
            
            cout<<"The students having even roll numbers are"<<endl;
     
     // How to call the functions according to my code.
     
          
          
          
          
          
           system("pause");
           return 0;
           
           
}
           


Answers

  •   
    #include <iostream>
    
    using namespace std;
    struct student      // defining a structure named student.
    {
           string name;
           int roll_no;
           int age;
    };
    
    void even(struct student a[])            //function for printing student names having even roll number.
                                                                           // Doubt in function variable (struct student a[]). 
    {       int i;
            /*
            You need to print the names. So, returning a[i] will 
            only return a student structure and not all the students.
            So, the best way is to print the name inside the function itself
            and use a return type of void
            */
            for( i=0;i<5;i++){if(a[i].roll_no%2==0)
            {
             cout<< a[i].name<<endl;
            }
            }
              // I think my return value is incorrect. 
            
    
    }
    void age14(struct student a[ ])   //function for printing student names having age 14.
    {        int i;
        /*
        Same logic here also
        */
             for( i=0;i<5;i++){if(a[i].age==14)
          {
           cout<< a[i].name<<endl;
          }
          }
          
    }
                /*
                According to the question, roll number should be passed
                */
    void detail(int roll, struct student a[])   // Function for getting the detail of a particular student.
    {
        for(int i=0;i<5;i++)
        {
            if(a[i].roll_no==roll)
            {
                  cout<< a[i].name<<endl;
                  cout<< a[i].roll_no<<endl;
                  break;
            }   
        }
    }
           
           
         
    
    
    int main(int argc, char *argv[])
    {   
        
        struct student a[5];
        /*your all functions are written for array of length 5*/
        for(int i=0;i<5;i++)      // loop for taking the input .
        {
                cout<<"Enter the name of the student"<<i+1<<endl;
                cin>>a[i].name;
                cout<<"Enter the roll number"<<endl;
                cin>>a[i].roll_no;
        }
                
                cout<<"The students having even roll numbers are"<<endl;
         
         // How to call the functions according to my code.
         even(a);
         //
         /*
            Right now, you are not storing the age of the students.
            You can call the function similary after completing it.
         */
          detail(2,a);    
              
              
              
              
               //system("pause");
               return 0;
               
               
    }
    

     


    • sir,why we use 'a' as the input while calling the function.As in even(a).
      - pawansaggu
    • There is no restriction to use only a, you can use any other variable name.
      - Amit Kumar

  •    seoexpertim

    I just found this blog and have high hopes for it to continue. Keep up the great work, its hard to find good ones. I have added to my favorites. Thank You. Download Kids Health and Diet Help For PC



  •    albinamuro2

    This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. It is the old what goes around comes around routine. pet rescue centers



  •    maxkvinton

     I can thank the team behind the service for writing research proposals enough. From the initial contact to the final delivery, the process was seamless. They exhibited a profound understanding of my research goals and translated them into a well-crafted proposal. The attention to detail, coupled with prompt communication, made this service stand out. I highly recommend dissertation proposal writing service to professionals seeking top-notch proposal writing assistance.



Ask Yours
Post Yours
Write your answer