How do I relate Two arrays together | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do I relate Two arrays together

I mean, lets say I have Students array with 5 names in it, say string Studs [5] ={"Dre","Lis", "john";"pat", "mark"}; and another array with Student marks in it, say int marks [5] ={15,23,70,45,86}; How do I make so that when I call, say, John..I automatically get his mark as well(70) thanks in adv

6th Nov 2016, 7:27 PM
Tawanda Mthembo
Tawanda Mthembo - avatar
4 Answers
+ 5
#include <iostream> using namespace std; struct test { string name[5]; int no[5]; }; int main() { test ob; int n; cout<<"enter name array "; for(int i=0;i<5;i++) cin>>ob.name[i]; cout<<"enter no array "; for(int i=0;i<5;i++) cin>>ob.no[i]; cout<<"enter no to be searched: "; cin>>n; for(int i=0;i<5;i++){ if(n == ob.no[i]){ cout<<"the respective name is :"<<ob.name[i]; return 0; } } cout<<"no not found "; return 0; }
7th Nov 2016, 3:49 AM
kamal joshi
kamal joshi - avatar
+ 2
You want to create structure or class Student. That would be the best way to do it. Structure is something that you also have in C. It's user defined type. So you found define new type called student that has two members, int mark and string name. Once you do it, you'll be able to create array of Students. More oop way is to created class Students, which would also have these two members. There are slight difference between structure and class, but I won't write that.
6th Nov 2016, 9:28 PM
Milica Todorovic
Milica Todorovic - avatar
+ 2
it can be done by switch,ifelse,structures,class.For basic level follow switch over if else as it takes more time to chech condition, can use structure easy and convinent
13th Nov 2016, 4:19 PM
Kuldeep Parashar
Kuldeep Parashar - avatar
0
I'd appreciate a Demo if possible
6th Nov 2016, 10:15 PM
Tawanda Mthembo
Tawanda Mthembo - avatar