Saturday, May 22, 2010

Pl suggest a C program for inserting and deletion of items in arrays?

The program should work in ANSI-C..

Pl suggest a C program for inserting and deletion of items in arrays?
Are you requesting a function or a program that insertes and deletes items in a C program . ?





//Program Name : EX0016.CPP


//Array Of Objects Method - 1





# include %26lt;iostream.h%26gt;


# include %26lt;conio.h%26gt;





class student


{


int stdno, marks;


char name[10];





public:


void get_data();


void display_data();


};





void student::get_data()


{


cout%26lt;%26lt;"\n Input Student Number := "; cin%26gt;%26gt;stdno;


cout%26lt;%26lt;"\n Input Student Name := "; cin%26gt;%26gt;name;


cout%26lt;%26lt;"\n Input Student Marks := "; cin%26gt;%26gt;marks;


}





void student::display_data()


{


cout%26lt;%26lt;"\n Student Number := "%26lt;%26lt;stdno%26lt;%26lt;endl;


cout%26lt;%26lt;"\n Student Name := "%26lt;%26lt;name%26lt;%26lt;endl;


cout%26lt;%26lt;"\n Studnet Marks := "%26lt;%26lt;marks%26lt;%26lt;endl;


}





void main()


{


clrscr();


student obj[2];


obj[1].get_data();


obj[2].get_data();


cout%26lt;%26lt;"\n Student Details";


cout%26lt;%26lt;"\n ==============="%26lt;%26lt;endl;


obj[1].display_data();


obj[2].display_data();


getch();


}








//Program Name : EX017.CPP


//Array Of Objects Method - 2





# include %26lt;iostream.h%26gt;


# include %26lt;conio.h%26gt;





class student


{


int stdno, marks;


char name[10];





public:


void get_data();


void display_data();


};





void student::get_data()


{


cout%26lt;%26lt;"\n Input Student Number := "; cin%26gt;%26gt;stdno;


cout%26lt;%26lt;"\n Input Student Name := "; cin%26gt;%26gt;name;


cout%26lt;%26lt;"\n Input Student Marks := "; cin%26gt;%26gt;marks;


}





void student::display_data()


{


cout%26lt;%26lt;"\n Student Number := "%26lt;%26lt;stdno%26lt;%26lt;endl;


cout%26lt;%26lt;"\n Student Name := "%26lt;%26lt;name%26lt;%26lt;endl;


cout%26lt;%26lt;"\n Studnet Marks := "%26lt;%26lt;marks%26lt;%26lt;endl;


}





void main()


{


clrscr();


int i;


student obj[2];


for (i = 1; i %26lt;= 2; i++)


obj[i].get_data();


cout%26lt;%26lt;"\n Student Details";


cout%26lt;%26lt;"\n ==============="%26lt;%26lt;endl;


for (i = 1; i %26lt;= 2; i++)


obj[i].display_data();


getch();


}
Reply:Viraj C, you used C++ in your example, he requested C.





Arrays are a set length. You don't really insert or delete items from the array. You can change the value. You cannot just say I want to delete the third item in an array of 10 items so the array is now only 9 items long. The closes thing you can do is dynamically create an array of the new size and copy the desired data from the old array and then free the memory used by the old array. Thats not a good way to do things.





Linked lists, vectors, etc are used to insert and delete items... they allow one to dynamically insert or delete objects in a data structure. However, because you are using C and not C++ you will have to create your own structs to link because you cannot use the STL.





Without knowing what you are doing, I cannot really offer any more advice.
Reply:um, i would use the vector class instead of arrays. then again, i dont know what your doing.


No comments:

Post a Comment