Can I merged arrays that have different data type? | SoloLearn: Learn to code for FREE!

+7

Can I merged arrays that have different data type?

like int, string and char? because we need to search, delete and update a value in the array which is based on user input

3/6/2021 5:03:38 AM

KIMBERLY PANGANIBAN

25 Answers

New Answer

+6

Ipang Create program that will accept user inputs to the following array, input will be inserted one at a time per array: string Name[n]; int age[n]; char gender; string course; then it will be displayed as | Name | Age | Gender | Course | | Kimb | 18 | F | BSIT |

+4

KIMBERLY PANGANIBAN You can use Structures to store different types of data into a single type.

+4

@I Am AJ ! Using structures would require you to merge two arrays of that structure type, but I get where you're coming from. It may also be possible to create an array of objects or unions and convert the source array types to these objects or unions, which is a similar concept to your suggestion.

+4

Kimberley Paganiban, In C++ we can create classes. Create a student class and an array of students. Better yet use a Vector of students. An array has a fixed size whereas a Vector can have items added and deleted. https://www.cplusplus.com/reference/vector/vector/ A vector is known as a container class. Other container classes include, lists, forward lists, queues, maps and others. Containers are part of the Standard Template Library (STL) in C++. https://www.cplusplus.com/reference/stl/

+4

Here is an example of using a vector of student objects in conjunction with a menu system. The solution is incomplete but it gives you some idea of the code structure required. https://code.sololearn.com/ca09A16a303A

+3

Dino Wun (Use the search bar plz!) She asked for different data type of array. KIMBERLY PANGANIBAN Array can be of same data type only. So you can't merge two different data type of array.

+3

KIMBERLY PANGANIBAN I don't understand, there is no menu for merging arrays. I see Insert, Search, Delete, Update record. Where is the need to merge the array I just don't get it.

+2

KIMBERLY PANGANIBAN I can see from your profile that you have completed the C++ course, congratulations on that. However, this appears to be a sizeable piece of code that is required, and unsuitable for the code playground's non interactive environment. It would help if you provided the entire question exactly as it is presented or a link to it. What you are attempting to do is create a database. A database deals in records. Each record is an independent data structure, not a fragment of an array. Is it is mandated by the assignment that the problem be solved in this way, or was this your own solution to the problem? If it must be completed using individual arrays for each property the code is going to be very messy. On the other hand if you are able to implement your own solution to the problem then using a struct or class and an STL container would be a more elegant solution.

+2

Kimberley, OK then. Your approach is not optimum. Using a different array for each field is inefficient. It is better to use a class or struct for each student and store the students in a collection class such as a Vector. To see an example of implementing a menu system take a look at this demo code I wrote a while ago... https://code.sololearn.com/cA16A15A2A25/#cpp By the way you don't have a menu option to display the data.

+1

Kimberley your post... Create program that will accept user inputs to the following array, input will be inserted one at a time per array: string Name[n]; int age[n]; char gender; string course; seems to be in error. The statement "Once the arrays are all set..." would indicate that the assignment wants you to use multiple arrays, one for each property. I suspect that it should read... string Name[n]; int age[n]; char gender[n]; string course[n]; This is not a logical way to organise the code as inserting and deleting entries would require you to perform operations across multiple arrays. It is far easier to use a struct or class and a single array of that data type. However, this may be an intentional step of the course to demonstrate how difficult this is compared with using a struct or class. With struct and class being covered later.

+1

Ipang Im sorry coz I dont really get it how can I search those records. Im still learning Martin Taylor It is not mandated to do it in this I just asked hehe, and it is just an activity given to us but I can't think of the solution

+1

KIMBERLY PANGANIBAN About searching records, we can simply use a loop to iterate through an array, we just break out of the loop once we find what we want. Search would be better and generally be faster if we also sort the array by specific field e.g. student role number or name, depending on what we search regularly.

+1

vaibhav what should I do?

0

ahm what if i need to merge all of the int, char and string?

0

okaay thankyou so much!

0

Can you give an example of the desired result and the source of data?

0

Once the arrays are all set, the user will be prompted with the following options: [1] Insert another record [2] Search a record [3] Delete a record [4] Update a record [5] exit the program

0

No. As work around, you can: * build an array (or other data structure) with a struct that holds 2 values of different type * or build a struct that holds two arrays depending on the approach (also you can use objects instead)

0

KIMBERLY PANGANIBAN I think you're asking it for your assignment and the menu or records you're talking about can be done with file handling easily , as I've already created a college management ( I think your assignment is also similar with it ) in C and C++ both.

0

KIMBERLY PANGANIBAN you should learn those concepts If I'm right about your assignment then it has involved following concepts Structure File handling Switches Loops Functions etc What can you do ? - you can learn it so that you can make it - in case of you've less time then you can follow any video tutorial - ask other for code ( but it's a learning plateform so you should ask only your closed ones ) - well I've the code already