Anda di halaman 1dari 9

http://answers.yahoo.com/question/index?qid=20100205220119AAIzQwc #include <iostream> #include <vector> using namespace std; void print(vector<int> v){ for(int i = 0; i < (int)v.

size(); i++){ cout<<v[i]<<" "; } cout<<endl; } int main(){ int size = 0, deleteItem = 0, insertItem = 0; char ch = '\0'; cout<<"Enter array size "; cin>>size; vector<int> v(size, 0); cout<<"Enter "<<size<<" elements "; for(int i = 0; i < size; i++){ cin>>v[i]; } while(true){ cout<<"Press d to display array elements, i to insert element, r to remove element or q to quit "; cin>>ch; if(ch == 'd'){ print(v); } if(ch == 'i'){ cout<<"Enter the item to be inserted "; cin>>insertItem; v.push_back(insertItem); cout<<insertItem<<" has been inserted to the list"<<endl; } if(ch == 'r'){ cout<<"Enter the item to be deleted "; cin>>deleteItem; for(int i = 0; i < (int)v.size(); i++){ if(v[i] == deleteItem){ v.erase(v.begin() + i); } } cout<<deleteItem<<" has been erased from the list"<<endl; } if(ch == 'q'){ return 0; } } return 0; }

http://www.dailyfreecode.com/code/performs-array-operations-488.aspx

Write a program that takes 2 array as an input and performs below operations 1) 2) 3) 4) 5) 6) Insert in an array Delete from an array Search an element Sort an array Merge 2 arrays Traverse / Display array values

write a program in such a way that array class is in a separate file and include that file in a new program file and perform operations using that program.
Download Sourcecode for Program that performs array operations like insert,delete, search, sort, merge and display (Size: 1.71 KB)

Code for Program that performs array operations like insert,delete, search, sort, merge and display in C++ Programming
// Program that performs array operations// Array classclass array{ int arrA[MAX],location,item,nA; int arrB[MAX],nB; int arr_merge[MAX+MAX],nM; public: array(){ location=0;item=0;nA=0; nB=0;nM=0; } void init(); //initial data assignmentvoid traverse(); //process is display (assumed)void insert(); void del(); void search(); void sort(); void merge(); }; void array :: init(){ clrscr(); cout<<"\n\n*****Initialize Array*****\n"; int choice,i; while(1){ cout<<"\n\n1) Array A\n"; cout<<"2) Array B\n"; cout<<"3) Array Merge\n"; cout<<"4) Return\n"; cout<<"Enter your choice : "; cin>>choice; switch(choice){ case 1 : cout<<"\n\n--Array A--\n"; cout<<"Total elements to be inserted : "; cin>>nA; for(i=0;i<nA;i++) cin>>arrA[i]; break; case 2 : cout<<"\n\n--Array B--\n"; cout<<"Total elements to be inserted : "; cin>>nB;

for(i=0;i<nB;i++) cin>>arrB[i]; break; case 3 : cout<<"\n\n--Array Merge--\n"; cout<<"Total elements to be inserted : "; cin>>nM; for(i=0;i<nM;i++) cin>>arr_merge[i]; break; case 4 : goto end; default : cout<<"\n\nInvalid Position\n"; } } end: } void array :: traverse(){ clrscr(); cout<<"\n\n*****Display Process during Traversing*****\n"; int choice,i; while(1){ cout<<"\n\n1) Array A\n"; cout<<"2) Array B\n"; cout<<"3) Array Merge\n"; cout<<"4) Return\n"; cout<<"Enter your choice : "; cin>>choice; switch(choice){ case 1 : cout<<"\n\n--Array A--\n"; for(i=0;i<nA;i++) cout<<setw(5)<<arrA[i]; break; case 2 : cout<<"\n\n--Array B--\n"; for(i=0;i<nB;i++) cout<<setw(5)<<arrB[i]; break; case 3 : cout<<"\n\n--Array Merge--\n"; for(i=0;i<nM;i++) cout<<setw(5)<<arr_merge[i]; break; case 4 : goto end; default : cout<<"\n\nInvalid Position\n"; } getch(); } end: } /*All Operations Except Merging are performed on arrA*/void array :: insert(){ clrscr(); int i; cout<<"\n\n*****Inserting Element*****\n"; if(nA >= MAX){ cout<<"\nArray is Full\nInsertion Not Possible\n"; goto end; } cout<<"\nEnter Location of insertion : "; cin>>location; location--; if(location<0 || location>=nA) { cout<<"\n\nInvalid Position\n"; goto end; } cout<<"Enter Item value to be inserted : "; cin>>item; for(i=nA-1;i>=location;i--){ arrA[i+1] = arrA[i]; } arrA[location]=item; nA++;

cout<<"\nItem is Inserted\n"; end: getch(); } void array :: del(){ clrscr(); int i; cout<<"\n\n*****Deleting Element*****\n"; if(nA < 0){ cout<<"\nArray is Empty\nDeletion Not Possible\n"; goto end; } cout<<"\nEnter Location of deletion : "; cin>>location; location--; if(location<0 || location>=nA) { cout<<"\n\nInvalid Position\n"; goto end; } cout<<"\nItem deleted is : "<<arrA[location]; for(i=location;i<nA;i++){ arrA[i] = arrA[i+1]; } arrA[nA-1]=0; nA--; end: getch(); } void array :: search(){ clrscr(); int i,found=-1; cout<<"\n\n*****Searching Element*****\n"; cout<<"\nEnter Item value to be search : "; cin>>item; for(i=0;i<nA;i++){ if(arrA[i] == item){ found=i+1; break; } } if(found==-1) cout<<"\nSearch NOT FOUND\n"; else cout<<"\nSearch is FOUND at "<<found<<" location\n"; getch(); } void array :: sort(){ clrscr(); int i,j,temp; cout<<"\n\n*****Sorting Element*****\n"; for(i=0;i<nA;i++){ for(j=i;j<nA;j++){ if(arrA[i] > arrA[j]){ temp = arrA[i]; arrA[i] = arrA[j]; arrA[j] = temp; } } } cout<<"\nData are Sorted\n"; getch(); } void array :: merge(){ clrscr(); int i,j; cout<<"\n\n*****Merging Arrays*****\n"; for(i=0;i<nA;i++)

arr_merge[i]=arrA[i]; for(j=0;j<nB;j++,i++) arr_merge[i]=arrB[j]; nM=nA+nB; cout<<"\nArrays are Merged\n"; getch(); } // Program that accesses array class and displays result. #include <iostream.h> #include <iomanip.h> #include <conio.h> #define MAX 10 #include "array.cpp"void main() { int choice; array obj; while(1){ clrscr(); cout<<"\t\tALL ARRAY OPERATIONS\n\n"; cout<<"\t\t1) Initialise Data\n"; cout<<"\t\t2) Traverse (Display Process)\n"; cout<<"\t\t3) Insert\n"; cout<<"\t\t4) Delete\n"; cout<<"\t\t5) Search\n"; cout<<"\t\t6) Sort\n"; cout<<"\t\t7) Merge\n"; cout<<"\t\t8) Exit\n"; cout<<"\t\tEnter your Choice : "; cin>>choice; switch(choice){ case 1 : obj.init(); break; case 2 : obj.traverse(); break; case 3 : obj.insert(); break; case 4 : obj.del(); break; case 5 : obj.search(); break; case 6 : obj.sort(); break; case 7 : obj.merge(); break; case 8 : gotoout; default: cout<<"\n\n\t\tInvalid Choice\n\n"; getch(); break; } } out: }

http://www.cplusplus.com/forum/beginner/75532/
struct student { string name; int student_number; bool operator< (student i, student j) { return (i.name<j.name); } } students[10];

// populate the array. Then do this to sort it: bool sorted = false; while (!sorted) { sorted = true; for (int i = 0; i < 9; ++i) { if (students[i] < students[i+1]) { student temp = students[i]; students[i] = students[i+1]; students[i+1] = temp; sorted = false; } } }

http://www.easyprogramming.net/tutorials/parallel_arrays_tutorial.php

//Programmer: //Website: //Program:

Nazmus EasyProgramming.net Array Tutorial #2 - Parallel Arrays

#include <iostream> #include <string> using namespace std; int main(){ string name[3]; int test1[3]; int test2[3]; float avg[3]; int i; for(i=0;i<=2;i++){ cout << "Please enter student's name: "; cin >> name[i]; cout << "Please enter first test score: "; cin >> test1[i]; cout << "Please enter second test score: "; cin >> test2[i]; } for(i=0;i<=2;i++){ avg[i] = (test1[i] + test2[i]) / 2; } cout << endl; cout << "Name" << " for(i=0;i<=2;i++){ cout << name[i] << " } " << "Average" << endl; " << avg[i] << endl;

system("pause"); }

http://cboard.cprogramming.com/c-programming/139869-modifying-parallel-arrays-arraysstructures.html

#include <stdio.h> // preprocessor directive to allow use of printf()/sca$ #include <string.h> //preprocessor directive to allow use of strings

//typedef below used to create a structure tag for structure definitions typedef struct person { //Algorithm step 2 char first_name[20]; //array "first_name"element

//Algorithm step 3 char last_name[20]; //array "last_name"element

//Algorithm step 4 char age[4]; //array "age" element

}ppl ; //tag name

void populate (ppl []); //PCF populate prototype void last_name_sort (ppl []); //PCF last_name_sort prototype

int main (void ) //Main function- returns and integer/ accepts no argumen$ { //Marks beginning of the main function's statement int i; //integer i created and initialized

//Algorithm step 1 completed in next four lines printf("Hello! \nThe purpose of this program is to use parallel arrays $ printf("order the names aphabetically (by last name) without mixing the$ printf("names and ages. The newly sorted information will be printed to$ printf("screen. \n\n\n\n");

ppl person[7]; //defined structure "person"

populate (person); //defined structure passed to PCF populate

last_name_sort (person); //defined structure passed to PCF last_name_s$ printf("\n\n<-------------------------~SORTED LIST~-------------------$ for (i=0; i<= 6; i++) { //Algorithm step 8 in for loop printf("%s %s %s\n", person[i].first_name , person[i].last_name, pe$ }

printf("\n\n\n"); printf("Thank you for using my program. I hope it was helpful!\n\n"); return (0) ; }

void populate (ppl person[]) { int i; //creation of integer variable i

printf("Please enter a first name, last name, and age with a space in b$ printf("and the first letter of the first and last name capitalized: \n$ printf("\n\n<-----------------------~UNSORTED LIST~--------------------$

for (i=0; i<= 6; i++) { scanf("%s %s %s",person[i].first_name , person[i].last_name, person[i].$ }

return; } void last_name_sort( ppl person[])

{ int i, j; char temp[20]; //creation of first temporary array for 3way switch of las$ char temp_1[20]; //creation of first temporary array for 3way switch of f$ char temp_2[4]; //creation of first temporary array for 3way switchof age

for (i=0; i<=5; i++) //outer for loop { for(j=0; j<=5; j++) //inner "nested" for loop { if (strncmp(person[j].last_name, person[j+1].last_name, 7) >0) { //Algorithm for last name sorting strcpy (temp, person[j].last_name); strcpy (person[j].last_name, person[j+1].last_name); strcpy (person[j+1].last_name, temp);

//Algorithm so first name follows where last name is sorted strcpy (temp_1, person[j].first_name); strcpy (person[j].first_name, person[j+1].first_name); strcpy (person[j+1].first_name, temp_1);

//Algorithm so age follows where last name is sorted strcpy (temp_2, person[j].age); strcpy (person[j].age, person[j+1].age); strcpy (person[j+1].age, temp_2); } }

return; } }

Anda mungkin juga menyukai