Insert new element to array and how shift char type

In summary, the conversation discusses how to insert a new element into an array of structs, how to copy a string into an array, and how to use strcpy. The solution involves shifting the existing elements in the array to make room for the new element, and using strcpy to copy the string into the array.
  • #1
Sumaya
29
0

Homework Statement



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



Homework Equations



insert another element into the array a.

The Attempt at a Solution





Code:
#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 a lot ...
 
Physics news on Phys.org
  • #2
Sumaya said:

Homework Statement



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



Homework Equations



insert another element into the array a.

The Attempt at a Solution





Code:
#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 a lot ...

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:
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.
 
  • #3
thanx a lot
Code:
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 ?
 
  • #4
Sumaya said:
thanx a lot


Code:
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;
You can't do this, and I explained why you can't in post #2.
Sumaya said:
suppose i want to insert in the front of the array .
I also explained how you can insert a student structure at any given position.
Sumaya said:
and how i can use strcpy ?
See the documentation - http://www.cplusplus.com/reference/clibrary/cstring/strcpy/
 
  • #5
hi

Code:
#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 ... thanks a lot ...
 

1. What is an array and how does it store data?

An array is a data structure that can store a collection of data elements of the same type. It is a sequential list of items that are accessed by an index. The data stored in an array can be of any data type, including characters.

2. How do you insert a new element into an array?

To insert a new element into an array, you need to first determine the index at which you want to insert the element. Then, you can use a loop to shift all the elements after the index to the right, making space for the new element. Finally, assign the new element to the desired index.

3. What is meant by "shifting" when inserting a new element into an array?

Shifting refers to the process of moving elements in an array to the right or left to make space for a new element. When inserting an element, all the elements after the desired index need to be moved one position to the right to make room for the new element.

4. Can you insert a character into an array of a different data type?

No, an array can only store elements of the same data type. If you want to insert a character into an array, it must be an array of characters (char type). Otherwise, you will need to convert the character to the appropriate data type before inserting it into the array.

5. What is the best way to insert a character into an array of characters?

The best way to insert a character into an array of characters is to use the array.splice() method. This method allows you to specify the index at which you want to insert the character and also handles the shifting of elements for you. Additionally, you can use the array.push() method to append the character to the end of the array.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
5K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Replies
10
Views
896
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Back
Top