Skip to main content

Data Structure In C++

There are a classroom where are hundred of student. They all have individual name, id, and age. If we want to collect their information we need to declare hundred of variables for name, hundred of variables for id, hundred of variable for age.  So we need to write a huge code for it. Array can help to reduce this code but when to want to update information one of student, it is hard.
By using structure we can solve this problem. Data structure is the collection of various kinds of data under the same name. it is one kinds of advanced array system.
Lets know more about data structure……
Firstly we observe the syntax of  data structure
Struct sructer_name{
Data_type1  data_name1;
Data_type2  data_name2;
Data_type3  data_name3;
.
.
Data_type(n) data_name(n);
}object_name
Lets to see a code to more learn about data structure. I think you are learn something from the syntax but it can’t  enough.

#include<iostream>
#include<string>

using namespace std;

struct info{
    string name;
    int age,id;
    float grads;
}student[100];

int main(){
        for(int i=0;i<2;i++){
            cout<<"\nName:  ";
            cin>>student[i].name;
            cout<<"\nAge:  ";
            cin>>student[i].age;
            cout<<"\nId:  ";
            cin>>student[i].id;
            cout<<"\nGrads:  ";
            cin>>student[i].grads;
        }
        for(int i=0;i<2;i++){
            cout<<"\nName: "<<student[i].name;
            cout<<"\nAge: "<<student[i].age;
            cout<<"\nId: "<<student[i].id;
            cout<<"\nGread: "<<student[i].grads;
            cout<<endl;
    }
    return 0;
}


At first at least two time observe this code ….
What did you learn from this ?
Ok ,
Struct info{}:  Firstly declare a structure name, which has four component. String type “name”, integer type “id” and “age” and floating point type grads.  Object name student[100].
Total structure works as array, which name is student. This is the main concept of structure, now we learn how to use structure. Object_name.data_name  is act as a variable . so Wright the code.




Popular posts from this blog

Switch, Case, Default Statements In C++

Switch case is uses as alternative of if….else. When we need to use long term of if…else then we use switch case for reducing program execution time. syntax: Switch(variable){ Case value 1: Statement; Break; Case value 2: Statement; Break;

do...while loop in C++

The do while loop is very similar to while loop . In the while loop first check final condition then execute the statement. But in the do while loop first execute the statement then check the final condition. Syntax: Initialization; Do{ increment/decrement;   } while(Final condition)

For Loop In C++

A loop can repeat the statement of a particular job continuously for a specific number of times. Loop Description Syntax For The condition can tested before statement execution.   For(Initialization, Final condition, increment /decrements ) { statement; } while First it check the condition then do the job. Initialization; While(Final condition) { Statement; increment /decrements; } Do..while First do job then test the condition. Initialization; Do{ Statement; increment /decrements; } While(Final condition)