Ask your own question, for FREE!
Mathematics 20 Online
OpenStudy (anonymous):

(sorry, the Computer Science forum isn't active) 2D ARRAYS IN C I want to save multiple-character words into a 2d array: char xyz [10][20] is it enough for me to do it like (if my word is assigned to a variable called abc): abc=xyz[0] or do i have to break it down to characters and put them indivudually into their respective places, such as: abc[0]=xyz[0][0]; abc[1]=xyz[0][1]; ... abc[N] = xyz [0][N] ?

hartnn (hartnn):

xyz[0] will be an array of 20 characters. abc isn't, right? so abc=xyz[0] will probably throw you a warning. or run-time error

OpenStudy (anonymous):

abc is a 1D array, such as: char abc[5] i would like to input 10 words into abc, each one into one row of xyz

OpenStudy (anonymous):

*into xyz

hartnn (hartnn):

Warning that i got , when abc was a char. "initialization makes integer from pointer without a cast " Let me try with abc as char array

OpenStudy (anonymous):

well, abc is a char array. actually, i want to do this with numbers but i will treat them as char arrays (e.g. char aa=501436)

hartnn (hartnn):

your input is filled in abc one-by-one and you want to fill the 2D char array, xyz, right? or the other way round?

hartnn (hartnn):

"save in 2D array" so shouldn't your question be xyz[0] = abc; whether that is legal or not...

hartnn (hartnn):

and to answer your question, you can fill xyz one char at a time, by using nested for.

OpenStudy (anonymous):

yes, that's what i meant

OpenStudy (anonymous):

what is "nested" for? (i know what for is)

hartnn (hartnn):

for inside a for, for(i = 0; i< 10; i++) { for(j=0; j<20; j++) { .... } }

OpenStudy (anonymous):

#hartnn can i also ask about the next step in my program?

hartnn (hartnn):

this explains any 2D array, quite nicely. http://stackoverflow.com/questions/26941470/initializing-2d-char-array-in-c

OpenStudy (anonymous):

so my program will probably use a do while to generate a number (given by the user) of unique words that will be saved into my 2d array. since the words need to be different, i need to scan the 2d array to find out whether the currently generated word already is in my memory. which function should i use? imagine i will have something like this: char xyz[3][5]= { a, p, p, l, e, t, r, e, e, -, c, a, r, -, -, } and my generated word will be apple. the problem is that the characters in xyz each fill one field and the rows don't really act like compact words

OpenStudy (anonymous):

(by the way, i got a one-week ban from stackowerflow for asking elementary questions so probably no help for me there)

OpenStudy (anonymous):

Try saving the word (characters) you want to check if it's in memory, then have some for-loop to see if any words in the 2D array start with the first letter, then second, third etc letter in the word you're looking up.

OpenStudy (anonymous):

@aleroth this would be a problem because i will work with words with unlimited length

OpenStudy (anonymous):

They can't be unlimited because your 2d array is limited?

OpenStudy (anonymous):

Your 2d array is given a fixed maximum number of characters, a 2d array looks like this: xyz[max number of words][max length of words]

OpenStudy (anonymous):

they will be kinda limited but the 2d array will be like xyz[desired number of words][1000]

OpenStudy (anonymous):

i have an idea, maybe i could use a nested loop with memcmp that would compare the current word with all the previous numbers and return 0 if the word hasn't been used yet, then the original loop would save the word into another row of the 2d array

OpenStudy (anonymous):

*with all the previous words

OpenStudy (anonymous):

well, is it even possible to compare something to a single row of a 2d array, for example compare char abc to the row xyz[0]?

hartnn (hartnn):

to compare a char array abc, with one of the entries of 2D array xyz[0], (A) compare it character by character using one for loop (B) memcmp

OpenStudy (anonymous):

i'll use a loop with memcmp, thanks :) by the way, @hartnn, i will continue programming straight away so can i tag you if i have any more questions?

hartnn (hartnn):

and i guess memcmp internally does char by char comparison only... just using a pre-defined function in the library...which is always better as the standard function might check for boundary cases which we could forget to check.

hartnn (hartnn):

sure, feel free to tag me. hope some C expert also contributes...even I am still a beginner :)

OpenStudy (anonymous):

@hartnn if i only want something to happen if a certain condition occurs, can i just put there an: if (my condition) {the action happens} and leave it like that, with no else?

hartnn (hartnn):

yes, most definitely.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!