Theory
कौन-सा mark?
2 students, 3 subjects, एक grid:
int marks[2][3] = {{70, 80, 90}, {60, 75, 85}};
marks[1][2] क्या है?
Think first
पहले आपका अंदाज़ा
आपके हिसाब से marks[1][2] में जो value है, वह type कीजिए।
Show the answer
85। पहला index row है और दूसरा column, दोनों 0 से: row 1 दूसरा student है, column 2 तीसरा subject। पहले row, फिर seat, cinema ticket की तरह।
Theory
Grid declare करना
int marks[3][4]; 3 rows × 4 columns = 12 int boxes reserve करता है। इसे हर row के लिए एक अंदर वाली brace से initialize कीजिए, जैसे ऊपर, और marks[i][j] को row i, column j पढ़िए। Memory में rows एक के बाद एक रहती हैं: पहले पूरी row 0, फिर row 1।
Think first
Size और आख़िरी box
int marks[4][5]; में कितने boxes हैं, और आख़िरी valid element कौन-सा है?
Show the answer
20 boxes (4 × 5), और आख़िरी है marks[3][4]: हर index अपने size से एक कम पर रुकता है। marks[4][5] दोनों दिशाओं में grid के बाहर है।
Practical
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++) {
for (j = 0; j < 3; j++)
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
Output बताइए
ऊपर वाले program का output हूबहू type कीजिए।
Show the answer
70 80 90
60 75 85
बाहर वाला loop row चुनता है, अंदर वाला उसके columns पर चलता है, और newline हर row को ख़त्म करता है: Unit 2 के nested loops, अब चलने के लिए एक grid के साथ।
Theory
Letters का grid
char names[3][10] = {"Riya", "Aman", "Zoya"}; 3 strings रखता है, हर row में 9 letters तक और उसका अपना '\0'। एक index पूरी string चुनता है (names[1] "Aman" है); 2 indexes एक letter चुनते हैं (names[1][0] 'A' है)।
Practical
Exam-style: output बताइए
#include <stdio.h>
int main() {
char names[3][10] = {"Riya", "Aman", "Zoya"};
printf("%s %c", names[0], names[2][1]);
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Quiz
Exam-style: ऊपर वाले program का output क्या है?
- Riya o
- Riya Z
- R o
- Riya m
Show the answer
Riya o
%s के साथ names[0] पूरी पहली string है, Riya। names[2][1] row 2 (Zoya) का letter 1 है, 0 से गिनकर, यानी o। दूसरे index को 1 से पढ़ें तो Z आता है।
Summary
Key takeaways
- int a[r][c] में r × c boxes होते हैं; a[i][j] यानी row i, column j।
- दोनों indexes 0 से शुरू होते हैं; आख़िरी box a[r-1][c-1] है।
- हर row के लिए एक अंदर वाली brace से initialize कीजिए; nested loops से चलिए।
- याद रखने का hook: पहले row, फिर seat, cinema ticket की तरह।