Theory
એક student એક line હતો; એક class એક grid છે
Marks array એ દરેક student નો એક number રાખ્યો. હકીકત: દરેક student ના 4 subjects છે.
60 students × 4 subjects એ boxes ની એક line નથી, એ એક grid છે: rows અને columns, બરાબર એ marksheet જે teacher noticeboard પર લગાવે છે.
C કોઈ array ને એક બીજો index આપીને grids બનાવે છે:
int marks[3][4]; /* 3 students, 4 subjects */
જો આમાં BCA102 ની matrices ની ગંધ આવે, તો તમારા નાક પર ભરોસો કરો, એ જ grid, હવે code માં.
Theory
Cinema hall
એક cinema ticket ક્યારેય "seat 47" નથી કહેતી; એ કહે છે row F, seat 9: પહેલા row, પછી એની અંદર seat. એક 2D array એ cinema hall છે: marks[1][2] નો અર્થ છે row 1, seat 2 (બંને zero થી ગણાય). BCA102 નો RC નિયમ અકબંધ બચે છે: પહેલા Rows, પછી Columns, એક RC car ની જેમ, અને હવે એ તમારા code ને ચલાવે છે.
Theory
Declaration અને initialization
- Declare:
int marks[3][4];3 × 4 = 12 int boxes reserve કરે છે. - Initialize, braces ની અંદર braces (દરેક row માટે એક inner brace):
int marks[2][3] = {
{70, 80, 90}, /* student 0 */
{60, 75, 85} /* student 1 */
};
- Access:
marks[1][2]એ student 1 (બીજો), subject 2 (ત્રીજો) છે: 85.
Memory માં grid row-દર-row store થાય છે (row-major): પહેલા row 0 ના બધા boxes, પછી row 1 ના, grid ના ચશ્માં પહેરેલી એક લાંબી line.
Practical
Nested loops થી grid પર ચાલવું
#include <stdio.h>
int main() {
int marks[2][3] = {{70, 80, 90}, {60, 75, 85}};
int i, j;
for (i = 0; i < 2; i++) { /* each student (row) */
printf("Student %d: ", i);
for (j = 0; j < 3; j++) /* each subject (col) */
printf("%d ", marks[i][j]);
printf("\n");
}
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Think first
Grid વાંચો
int marks[2][3] = {{70, 80, 90}, {60, 75, 85}}; વાપરીને કાઢો: marks[0][1], marks[1][0], અને marks[1][2]. પહેલા rows, zero થી.
Show the answer
marks[0][1] = 80 (પહેલો student, બીજો subject). marks[1][0] = 60 (બીજો student, પહેલો subject). marks[1][2] = 85 (બીજો student, ત્રીજો subject).
જો તમે 60 અને 80 ઉલટાવ્યા, તો તમે column-પહેલા વાંચ્યું, સૌથી સામાન્ય 2D ભૂલ. Ticket નિયમ: row, પછી seat.
Quiz
int marks[4][5] કેટલા int boxes reserve કરે છે, અને છેલ્લો valid element કયો છે?
- 20 boxes; છેલ્લો marks[3][4] છે
- 20 boxes; છેલ્લો marks[4][5] છે
- 9 boxes; છેલ્લો marks[3][4] છે
- 20 boxes; છેલ્લો marks[4][4] છે
Show the answer
20 boxes; છેલ્લો marks[3][4] છે
કુલ = rows × columns = 4 × 5 = 20. બંને indexes zero થી શરૂ થાય છે, તો એ rows-1 અને cols-1 પર પૂરા થાય છે: marks[3][4]. marks[4][5] ને element તરીકે લખવું એકસાથે બે dimensions માં out-of-bounds પાપ કરે છે. Size ગુણાય છે; છેલ્લો index દરેકમાંથી એક બાદ કરે છે, બે નિયમ, એક સવાલ.
Theory
characters નું એક grid: names ની એક list
Rows text પણ રાખી શકે છે:
char names[3][10] = {"Riya", "Aman", "Zoya"};
દરેક ROW એક char array છે: 9 સુધી letters અને પોતાનું '\0'. તો names[1] આખી string "Aman" છે, અને names[1][0] એ letter 'A' છે.
એક index string પસંદ કરે છે, બે indexes letter પસંદ કરે છે, marks program આખરે store કરી શકે છે કે કોણે શું score કર્યું, names અને numbers અડોઅડ.
Watch out
Marks ક્યાં કપાય છે
Column-પહેલા વાંચવું: marks[i][j] એ row i, column j છે, હંમેશા; એમને ઉલટાવો અને દરેક જવાબ transpose થઈ જાય છે. Comma indexing: marks[1, 2] C માં compile થાય છે પણ એનો અર્થ row 1 column 2 નથી (છાનેમાને એનો અર્થ marks[2] થાય છે, એક સાચો trap); હંમેશા marks[1][2] લખો. બંને dimensions માં bounds: rows-1 અને cols-1 એ છેડા છે. અને એક char 2D array ની દરેક row ને પોતાનું '\0' space જોઈએ.
Theory
BCA102 એનું ફળ આપે છે
તમે આ grid નું ગણિત પહેલેથી જાણો છો: order m×n, aᵢⱼ addressing, BCA102 ના matrix lessons. આગળનો lesson એ ગણિતને C માં ફેરવે છે: બે marks grids ઉમેરવા, transpose કરવું, અરે matrix multiplication સુધ્ધાં, બધું 2D arrays પર nested loops. તમારા બે subjects હમણાં એક જ skill માં ભળી ગયા.
Summary
Key takeaways
- int marks[3][4]: 3 rows × 4 columns = 12 boxes; પહેલા rows, પછી columns.
- બંને indexes zero થી શરૂ; છેલ્લો element [rows-1][cols-1] છે.
- braces ની અંદર braces થી initialize કરો, દરેક row માટે એક inner brace.
- Nested loops થી grids પર ચાલો: outer i = rows, inner j = columns.
- char names[3][10]: ત્રણ strings, દરેક row પોતાના '\0' સાથે.
- ક્યારેય comma થી index ન કરો: marks[1][2], નહીં કે marks[1, 2].
- યાદ રાખવાની યુક્તિ: cinema ticket, પહેલા row, પછી seat.