1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Insert new element to array and how shift char type

  1. Oct 9, 2011 #1
    1. The problem statement, all variables and given/known data

    Code (Text):
    struct student
    {
    char name[20];
    int id;
    float gpa;
    };
    student a[20];


    2. Relevant equations

    insert another element into the array a.

    3. The attempt at a solution



    Code (Text):


    #include<iostream>
    using namespace std;
    #include <stdio.h>
    #include <string.h>

    struct student
    {
        char name[20];
        int id;
        float gpa;
    };
    student a[20];
    int main()
    {
        int item,newKey,posn,newGpa;
        int ID;
        char newName[20];
        const int NUMEL=30;
        student a[NUMEL]={{"Sarah",300,77.5},{"Joerj",600,87.9},{"Steven",400,89},{"Jason",200,65.7},{"Kyle",500,99},
                           {"Adam",800,69.44},{"Alix",700,76.9},{"Jermey",900,85.8}};        

        int sumSize;
        for(int i=0;a[i].id!=NULL;i++)
        {
            sumSize=i;
        }
        int actualSize=sumSize+1;
        cout<<"The actual size (no. of elements ) of the array is  :  "<<actualSize<<endl<<endl;
        cout<<"The free size in the static array is  :  "<<NUMEL-actualSize<<endl<<endl;
       
        cout<<"the position of the new data will be inserted in the front (first element)"<<endl;
        cout<<" please, enter the positon : "<<endl;
        cin>>posn;
        --posn;  
        //cout<<"Input the name :"<<endl;
        //cin>>newName;

        cout<<" the new id is "<<endl;
        cin>>ID;
        cout<<"enter the new gpa :"<<endl;
        cin>>newGpa;

        cout<<"enter the new name :"<<endl;
       

    for(int i=actualSize;i>posn;i--)
    {
         a[i].name=a[i-1].name; \\ it doesn't accept = for char ... why ?? how i can do it?? doing shifting from the end to insert new element ..


        a[i].id=a[i-1].id;
        a[i].gpa=a[i-1].gpa;
       

    actualSize++;
    }
    for(int i=0;i=posn;){

        cin>>a[i].name;
    }
    for(int i=0;i=posn;)
    {
        cout<<a[i].name<<"  "<<endl;
    }
        //newName[i]=a[i].name;// here also
        //a[posn].name=newName;
        //}
            //a[posn].name=newName;
            a[posn].id=ID;
            a[posn].gpa=newGpa;
    //cout<<a[posn].id<<endl<<endl;
    cout<<"the new array after inserstion  "<<endl;
    for(int i=0;i<actualSize;i++)
    {
        cout<<a[i].name<<"  "<<a[i].id<<"  "<<a[i].gpa<<endl;
    }


        return 0;
    }


     


    i want to know how to insert char name into the array and doing shift for whole names ...

    thanx alot ...
     
  2. jcsd
  3. Oct 9, 2011 #2

    Mark44

    Staff: Mentor

    I think your questions are
    1) How to insert a student struct into the array of structs.
    2) How to copy a string into an array.

    For 1, if you want to insert a student struct into the array at index j, you need to shift the existing elements of the array one element higher in the array. For example, if you want to put a new element at index 4, you need to move all of the elements currently at indexes 4, 5, 6, and so on to indexex 5, 6, 7, and so on. One difficulty is that your array is defined with 20 elements, so if the array is full, shifting all the elements one element higher in the array will cause the loss of the element at index 19 (the highest index in your array).

    If you do things this way, you want to start at index 18, and move it to index 19, then move the element at index 17, and move it to index 18, and so on down to the index of the element you want to free up.

    For 2, since you are using the C standard library, to copy a string from one place to another, use strcpy. The prototype of this function is in string.h.

    The reason what you were trying didn't work is that you can't copy an array of char using assignment.

    Code (Text):
    newName[i]=a[i].name;
    There are two problems with this code.
    a) The types on either side are different. newName is type char. a.name is type char [], or character array.
    b) The name of an array doesn't represent all of the data stored in the array (a string of characters, in this case). It evaluates to the address of the first element in the array. In other words, name by itself is a kind of pointer, which you might not have studied yet.
     
  4. Oct 10, 2011 #3
    thanx alot


    Code (Text):

    for(int i=actualSize;i>posn;i--)
    {
    a[i]=a[i-1];

    actualSize++;
    }
     
    i did this to shift elements and without getting problem with name .

    but how i say that a[0].name=newName;
    suppose i want to insert in the front of the array .
    and how i can use strcpy ?
     
  5. Oct 10, 2011 #4

    Mark44

    Staff: Mentor

    You can't do this, and I explained why you can't in post #2.
    I also explained how you can insert a student structure at any given position.
    See the documentation - http://www.cplusplus.com/reference/clibrary/cstring/strcpy/
     
  6. Oct 10, 2011 #5
    hi

    Code (Text):

    #include<iostream>
    using namespace std;
    #include <stdio.h>
    #include <string.h>

    struct student
    {
        char name[20];
        int id;
        float gpa;

    };
    student a[20];
    int main()
    {
        int item,newKey,posn,newGpa;
        int ID;
        const int NUMEL=30;
        student a[NUMEL]={{"Sarah",300,77.5},{"Joerj",600,87.9},{"Steven",400,89},{"Jason",200,65.7},{"Kyle",500,99},
                           {"Adam",800,69.44},{"Alix",700,76.9},{"Jermey",900,85.8}};        

        int sumSize;
        for(int i=0;a[i].id!=NULL;i++)
        {
            sumSize=i;
        }
        int actualSize=sumSize+1;
        cout<<"The actual size (no. of elements ) of the array is  :  "<<actualSize<<endl<<endl;
        cout<<"The free size in the static array is  :  "<<NUMEL-actualSize<<endl<<endl;
       
        cout<<"the position of the new data will be inserted in the front (first element)"<<endl;
        cout<<" please, enter the positon : "<<endl;
        cin>>posn;
        --posn;  
       

       

    for(int i=actualSize;i>posn;i--)
    {
    a[i]=a[i-1];
       
    actualSize++;
    }
    cout<<"enter the new name :"<<endl;
        cout<<" the new id is "<<endl;
        cout<<"enter the new gpa :"<<endl;
    for(int i=0;i<=posn;i++)
    {

        cin>>a[i].name;\\ here was my mistake to insert new values
        cin>>a[i].id;
        cin>>a[i].gpa;

    }

    cout<<"the new array after inserstion  "<<endl;
    for(int i=0;i<actualSize;i++)
    {
        cout<<a[i].name<<"  "<<a[i].id<<"  "<<a[i].gpa<<"  "<<endl;
    }


        return 0;
    }

     
    thank you for helping me .. i solved the problem ... thanx alot ...
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Insert new element to array and how shift char type
  1. Shifting an array in C (Replies: 4)

Loading...