How to initialize (i.e. set) all elements of a 2D array with user input in C. Source code: github.com/por.... Check out www.portfolioc... to build a portfolio that will impress employers!
Пікірлер
@INJANAY-r2pАй бұрын
i was trying to make a 2d array in which the user gives us input. bro chatgpt took so complicated paths to achieve the same thing. after watching you i was also able to do the same thing with much ease. thansk.
@bashithaperera72283 жыл бұрын
This was very helpful. Thank you!
@PortfolioCourses3 жыл бұрын
You're welcome! :-D
@IMdrummerTab Жыл бұрын
so awesome. im buying your udemy class now hope to see more portfolio buildi projects
@PortfolioCourses Жыл бұрын
I'm glad you enjoyed the video, and thank you for your support! :-)
@KendrewLok2 жыл бұрын
Hello I have a question. Let's say I have an array[2][2] and the scanf() will read row inputs in a single line. For row 0 , I type 1 2 3, and row 1 I type 4 5, I will end up with an array of {{1, 2},{3, 4}}. What I want is {{1, 2},{4, 5}}, so '3' should be discarded as each row should only have 2 elements, is there a way to deal with this? Thanks
@PortfolioCourses2 жыл бұрын
Great question Kendrew! 😀 The reason that's occurring is that the first scanf reads two ints from the standard input stream (i.e. the terminal the user is typing into) and then one int is left on the stream. The next scanf() then reads this int, before reading any additional user input provided. So what you could do is after the first scanf() is clear the standard input stream. You can do this simply with: fflush(stdin); Technically speaking fflush() is intended for clearing output streams in the C standard, but mainstream C compilers will support using it this way too. There are other more portable ways to clear the standard input stream if you're curious, but I'd suggest just using the above method: www.geeksforgeeks.org/clearing-the-input-buffer-in-cc/.
@AskingIse2 ай бұрын
can i ask what compiler did you use?
@demonthor24652 жыл бұрын
This is very helpful..thank u sir. 😇
@PortfolioCourses2 жыл бұрын
You're very welcome! 😀
@moses9187 Жыл бұрын
quick and simple thank you
@PortfolioCourses Жыл бұрын
You’re welcome! :-)
@juliusphung2 жыл бұрын
Thank you for a very informative tutorial video. I have been searching information about writing a 2D array to a FILE and printing it from a FILE. Do you think it is possible to do so even my 2D array is not full of elements. The reason is that I am making a study schedule with 2D array[date][time] of struct., and ofc there might be sometimes for a break so that [index][index] spot would be blank . If it is possible, do you have any advice to print the whol time table ? Besr regards
@PortfolioCourses2 жыл бұрын
Great question Julius! :-) And yes, there is a lot of ways you could do that I suspect. For example, you could have a bool struct member "available" that is true or false whether or not the time slot is filled or not. Then when you print out the 2D array, and you encounter a time slot that is available (say because available is set to true), you could output a "blank space" into the schedule in place of whatever else would normally be output. That's just one idea though, there would be a lot of good ways to do something like this.
@juliusphung2 жыл бұрын
@@PortfolioCourses Thank you so much for your reply ! I actually encountered an obstacle about 2D array. Let say I have a struct type containing name of the course and name of the room. My idea is using 2D array that has fixed size of [9][5], 9 stands for time from 09:00 to 17:00 and 5 stands for day from Monday to Friday, as a paramater into a function that adding users' input (name of course and name of the room ) , also time and day inputs to decide which index of 2D array that the course belongs to. I tried to use pointer to 2D array to work for this idea, but I did not successed. Compiler printed error as "Expression must be a modifiable lvalue" Do you have any hint for me ? I have been searching about this on the internet for few hours bu still not much material / sources related to my probem 😅😅😅😅 Brs
@PortfolioCourses2 жыл бұрын
@@juliusphung If you want to pass a 2D array like that to a function you would need to have a 2D array parameter with the dimensions set as well, this video might help: kzbin.info/www/bejne/kIazl4qwidGDmKc.
@juliusphung2 жыл бұрын
@@PortfolioCourses thanks for your reply . I have just watched the vidoe that you recommended . Do you suggest that I should use 2d array instead of a pointer that points to a 2D struct array ? Would using 2d array as function paramter easier?
@PortfolioCourses2 жыл бұрын
Yes, I think it would be much easier to use a 2D array as a function parameter. :-)
@احمدفايز-ه3ي2 жыл бұрын
I’m still learning how to use c, so I’m sorry if the question is weird. I don’t get how the compiler is knowing the number of rows and cols in your example! Thnx for the video, it was perfect.
@PortfolioCourses2 жыл бұрын
You're welcome! 😀 And the compiler knows the number of rows and cols in the array because it is declared with a certain number of rows and cols: int array[ROWS][COLS]; Where ROWS and COLS are preprocessor constants defined here: #define ROWS 3 #define COLS 5
@karenvaldez22402 жыл бұрын
Hi! Thanks for the video, it was really helpful. How could it be done if I need to initialize an array one by one? For example, I have a menu and one option says "Add a student", and if that option is selected, the it needs to fill "Name" and "Age", which both of these will be in the first element of the array, once it´s done, it will go back to the menu, asking again to enter an option. Also, hope you're having a good day/night!
@PortfolioCourses2 жыл бұрын
Hi Karen, great question, yes it could be done. It sounds like you would need a variable to keep track of how many students have been entered so far. And then each time the user adds a student, you would set the data in the 2D array using that variable as an index into the 2d array, and then increment that variable to keep track of the next index to use. 🙂
@rafacastellano2 жыл бұрын
great video, could you explain a way to do this but give the user the option to choose the number of rows columns? for instance, i am doing an assignment where there is a sort of terrain 'grid' made for finding treasures etc, but the user needs to choose the size of terrain (for example, user input 6 8 would generate a terrain grid with 6 rows and 8 columns)
@PortfolioCourses2 жыл бұрын
Thanks Rafael! What you'll need to do is use something like scanf() to store row and column dimensions entered by the user, and then use dynamic memory allocation to allocate space for a 2D array based on those values. I don't have a video on dynamic memory allocation for 2D arrays yet, but this will show you how to use dynamic memory allocation for 2D arrays: www.geeksforgeeks.org/dynamically-allocate-2d-array-c/. Good luck! 😀
@rafacastellano2 жыл бұрын
@@PortfolioCourses thank you for the quick and helpful answer!
@PortfolioCourses2 жыл бұрын
You’re welcome! :-)
@kizki23513 жыл бұрын
Very good video , but what do if we also wanna make row and cols with user input ? Can you please explain it to me ?
@PortfolioCourses3 жыл бұрын
What do you mean make rows and cols with user input? Something like this? Enter row 1: 5 10 3 9 5 Where the user is prompted to enter a row, and then the user enters all the values for the row at once? I might be able to help but I'm just trying to understand first. :-)
@kizki23513 жыл бұрын
@@PortfolioCourses Yep. The user will be asked to enter row and col numbers. Therefore it gives the user a complete control over the Matrix. It is basically 90% what you did but i am trying to figure out a way to let the user , choose the number of rows and cols
@sino17482 жыл бұрын
@@PortfolioCourses I would like to know how to do what you are mentioning, but with a character array Enter row 1: red enter row 2: blue
@PortfolioCourses2 жыл бұрын
@@kizki2351 that would be a bit tricky, you would need something like a scanf that reads input in a loop to read a variable amount of input. But it should be possible, I may try to make a video on this.
@PortfolioCourses2 жыл бұрын
@@sino1748 That should be possible with scanf/fgets reading in a string of input from the user and storing it into the array within the 2d array.
@hazlibinhayatstudent91752 жыл бұрын
Hi sir...firstly sorry for my rusty and bad English...your video is easy to understand, I had an assignment and that required me to fill up the football match table such as game played, game won,draw and so on...because im new in c language, can you explain how to done this...i dont have any i idea what to do..just give some idea to start. This some part of the question : NONE OF THE COLUMNS OF WIN, DRAW AND LOSE CAN HAVE VALUES MORE THAN 3 AS THE NUMBER OF GAMES PLAYED IS 3(MAXIMUM)...i realy need some guidance to done this. TQ in advance Sir
@PortfolioCourses2 жыл бұрын
I think I would need more details on the question to help you out. Is it columns of a 2D array that are being set? Are they supposed to be randomly set, or from user input? If it is from user input, then what should happen if someone enters more than the maximum?
@PortfolioCourses2 жыл бұрын
@@hazlibinhayatstudent9175 If you can paste it in here I might be able to help, but I'm not able to help people over e-mail right now.
@hazlibinhayatstudent91752 жыл бұрын
@@PortfolioCourses ok sir ill try
@hazlibinhayatstudent91752 жыл бұрын
@@PortfolioCourses this is the question sir: There are four teams that are playing football for their respective schools. For each of the teams, the information is to be inserted into the table 1 below. Table 1: Football Scores Team Games Played Win Draw Lose Goals Points A 3 B 3 C 3 D 3 There will be total of three games played, (as there are four teams, and each team will play with another team once only). i. You are given the task to create a program using C language, to input the information for the columns Win, Draw, Lose, Goals and to calculate the Points. You must use a two-dimensional (2-D) array for this question. ii. There are some rules which must be followed for the input: a) None of the columns of Win, Draw and Lose can have values more than 3 as the number of games played is 3 (maximum) b) The row Win, Draw and Lose must total to 3 for each Team. c) The total for column Draw must be even value, as there must be two teams that draw against each other. d) The points are to be calculated based on this: • Wins equals to 3 points • Draw equals to 2 points • Lose equals to 0 points So, if a team wins 2 games and loses one game, then the points obtained is 3 * 2 + 0 * 1 = 6 points. iii. After this, the football scores are kept in a scores.dat file which must have the team name, the number of wins, draws, losses, goals and points.
@PortfolioCourses2 жыл бұрын
@@hazlibinhayatstudent9175 OK it looks like what you need to do is make an int array with 3 rows (one for each time) and 4 columns (win, draw, lose, goals). So something like this: int standings[3][4]; And then you'll need to implement all of the logic to ensure that the rows and columns add up in the way that is expected given those restrictions. That's more than I am able to help you with, but given the question it really sounds like you will want to use an int array like the above.
@saswatsingh98432 жыл бұрын
Thank you sooo much brother
@PortfolioCourses2 жыл бұрын
You're welcome Saswat! 🙂
@sabeshroshan3881 Жыл бұрын
How to print particular row,cloum
@PortfolioCourses Жыл бұрын
I don't have a video on that topic yet, but I think I might make a video on that topic. :-) This video might help: kzbin.info/www/bejne/oYaQkIqGfqZmr9U. Instead of two loops, you would only need one loop. And instead of changing both the row and column with two counter variables, we would only change one, either the row or the column, and the other index would be fixed to the row or column we wish to print.
@ryanalnaser9142 жыл бұрын
whatever anything I do or did or know or did not knew or I do anything now or anything
@luisjasso5940 Жыл бұрын
Thanks, it helped me a lot. I want to do the exact same thing, but instead of using numbers, I'm using characters (not strings, just letters). The thing is that it gets stucked in the nested for, but when I introduce the letters manually, they're printed with no problems. Hope you can help me and tnxs again. #include #include int main () { int i, j; char letters[2][3]; //char letters[2][3] = {{'a','b','c'},{'d','e','f'}}; for(i=0;i
@PortfolioCourses Жыл бұрын
To read in each char, you will need to use %c in scanf() instead of %s. %c is for reading a char, %s is for reading a string. After you read in each character, after the call to scanf() you will need to clear the input buffer. This video shows you how to do that: kzbin.info/www/bejne/hGiQfqibgJd2jpo. But basically you could put this right after the scanf() call: int c; while ( (c = getchar()) != ' ' && c != EOF) {} and that should do it. You could also use another function entirely like getchar() to read a char instead of scanf(). Hopefully this helps! :-)