Theory
જે ગણિત તમે પહેલેથી પાસ કરી ચૂક્યા
BCA102 માં તમે matrices ઉમેરી, transpose કરી, અરે ગુણી સુધ્ધાં, કાગળ પર, marks માટે.
આજે એ જ operations C syntax પહેરીને પાછા આવે છે, અને આ રહ્યું મુક્ત કરનારું રહસ્ય: દરેક 2D operation બસ એક અલગ one-line body વાળા nested loops છે.
Addition? Nested loops, body ઉમેરે છે. Transpose? Nested loops, body indexes flip કરે છે. Multiplication? એક extra loop. જો તમે for (i...) for (j...) લખી શકો, તો તમે આ બધા લખી શકો.
Theory
એ જ tour, અલગ કામ
Nested loop ને એક tour route સમજો જે grid ના દરેક cell ને visit કરે છે, row-દર-row, seat-દર-seat. Route ક્યારેય બદલાતો નથી; ફક્ત દરેક seat પર કામ બદલાય છે. Addition: "જોડિયા cells ઉમેરો, પરિણામ અહીં મૂકો". Transpose: "આ cell ને એની અરીસાવાળી seat પર copy કરો". Search: "શું આ એ જ value છે જે આપણને જોઈએ છે?". એક route, ઘણા કામ, એ માનસિક model પાંચ exam programs ને એકમાં સમેટી દે છે.
Theory
Addition, subtraction, transpose, search
એક જ order વાળા grids a અને b માટે (BCA102 નો નિયમ બચે છે: ઉમેરવા માટે એક જ order):
c[i][j] = a[i][j] + b[i][j]; /* addition */
c[i][j] = a[i][j] - b[i][j]; /* subtraction */
Transpose (rows columns બની જાય છે, m×n, n×m બની જાય છે):
b[j][i] = a[i][j]; /* indexes swap roles */
એક value માટે Search: દરેક cell નો tour કરો, અને match પર બંને coordinates જણાવો:
if (a[i][j] == target) printf("at row %d col %d", i, j);
Practical
બે test grids ઉમેરવા (2 students × 3 subjects)
#include <stdio.h>
int main() {
int t1[2][3] = {{70, 80, 90}, {60, 75, 85}};
int t2[2][3] = {{75, 82, 88}, {65, 70, 90}};
int total[2][3];
int i, j;
for (i = 0; i < 2; i++)
for (j = 0; j < 3; j++)
total[i][j] = t1[i][j] + t2[i][j];
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) printf("%d ", total[i][j]);
printf("\n");
}
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Theory
Multiplication: ત્રીજો loop
BCA102 એ તમને શીખવ્યું: c નો cell (i, j) = a ની row i, b ની column j માં, જોડી ગુણો, એમને ઉમેરો. "એમને ઉમેરો" પોતે એક loop છે, એટલે multiplication ને ત્રણ જોઈએ:
for (i = 0; i < m; i++)
for (j = 0; j < p; j++) {
c[i][j] = 0; /* start the sum */
for (k = 0; k < n; k++)
c[i][j] += a[i][k] * b[k][j];
}
નવો counter k એ વહેંચાયેલા (inner) dimension પર ચાલે છે: a ની row i સાથે અને સાથોસાથ b ની column j ની નીચે. ઉમેરતાં પહેલાં c[i][j] = 0 ભૂલવું એ ક્લાસિક bug છે.
Think first
Transpose ને trace કરો
a એ 2×3 છે rows {1, 2, 3} અને {4, 5, 6} સાથે. દરેક cell પર b[j][i] = a[i][j] લગાવો. b શું છે, અને એનો order શું છે?
Show the answer
b એ 3×2 છે:
1 4
2 5
3 6
a ની row 0 (1, 2, 3) ઊભી થઈને b ની COLUMN 0 બની ગઈ. Index swap b[j][i] = a[i][j] બધું કામ કરે છે; loops ફક્ત tour કરે છે. જો તમારો b, 2×3 નીકળ્યો, તો તમે એને જૂના order થી declare કર્યો, transpose declaration ને પણ flip કરે છે.
Quiz
multiplication code માં, ત્રીજો counter k ખરેખર કોની સાથે ચાલે છે?
- વહેંચાયેલો inner dimension: a ની row i સાથે અને b ની column j ની નીચે, બંને સાથે
- result matrix c ની rows
- result matrix c ની columns
- બંને matrices ના બધા elements આડેધડ
Show the answer
વહેંચાયેલો inner dimension: a ની row i સાથે અને b ની column j ની નીચે, બંને સાથે
એક result cell માટે, k, a[i][k] ને b[k][j] સાથે જોડી બનાવે છે: a ની row i નો k-મો element, b ની column j ના k-મા element સાથે, ગુણી અને જમા. એ જ બરાબર BCA102 નો row-into-column છે, યાંત્રિકૃત. i અને j પસંદ કરે છે કયો cell; k એને COMPUTE કરે છે. દરેક loop ના કામ ને નામ આપવું બરાબર એ રીતે છે જેમ examiners એને phrase કરે છે.
Quiz
બે char arrays માં "Ri" અને "ya" છે. એમને એક array માં "Riya" રાખવા માટે merge કરવા, program પહેલી string copy કરે છે, પછી...
- બીજી string ને પહેલી ના '\0' position પર લખવાનું શરૂ કરે છે, અને પરિણામ ને terminate કરે છે
- બીજી string ને પહેલા array ના છેલ્લા box પછી લખે છે
- બંને strings ના ASCII codes ઉમેરે છે
- + operator વાપરે છે: "Ri" + "ya"
Show the answer
બીજી string ને પહેલી ના '\0' position પર લખવાનું શરૂ કરે છે, અને પરિણામ ને terminate કરે છે
Merge (concatenation) નો અર્થ છે પહેલી string ના terminator ને બીજી string ના પહેલા letter થી overwrite કરવું, આગળ copy કરવું, પછી એક તાજા '\0' થી બંધ કરવું. C માં strings માટે કોઈ + નથી (option D બકવાસમાં compile થાય છે); આ loop નું ready-made version strcat છે, Unit 4 માં આવી રહ્યું. '\0' નો હિસાબ એ જ આખી રમત છે.
Watch out
Marks ક્યાં કપાય છે
k-loop ની પહેલાં c[i][j] = 0 ભૂલવું: garbage તમારા સરવાળામાં ભળી જાય છે. Order ના નિયમ BCA102 થી આવે છે: addition ને એકસરખા orders જોઈએ; multiplication ને a ની columns = b ની rows જોઈએ, exam જવાબોમાં આ ચકાસણી જણાવો. Transpose ખોટું declare કરવું: એક m×n, n×m array માં transpose થાય છે. અને merge માં, છેલ્લો '\0' છોડવો એવી string છોડે છે જે ક્યારેય પૂરી થતી નથી, printf %s ખુશીથી સાબિત કરી દેશે.
Theory
unit નો બીજો અડધો ભાગ રાહ જુએ છે
હવે તમે grids પર હુકમ ચલાવો છો: બનાવવું, tour, ઉમેરવું, flip, search, ગુણવું. જે તમે હજી નથી જાણતા એ છે કે array નું નામ ખરેખર છે શું, અને scanf ને એક માટે ક્યારેય & કેમ જોઈતું ન હતું. જવાબ, addresses, C નો સૌથી ડરામણો અને સૌથી interview માં પૂછાતો topic ખોલે છે: pointers. આગળનો lesson એને એક ચિત્રથી નિષ્ક્રિય કરી દે છે.
Summary
Key takeaways
- બધા 2D operations અલગ bodies વાળા nested loops છે (tour અને કામ).
- Addition/subtraction: એક જ order વાળા grids પર cell-દર-cell: c[i][j] = a[i][j] ± b[i][j].
- Transpose: b[j][i] = a[i][j]; m×n, n×m બની જાય છે.
- Multiplication: ત્રણ loops; k વહેંચાયેલા dimension પર ચાલે છે; પહેલાં c[i][j] zero કરો.
- Search બંને coordinates જણાવે છે; string merge '\0' ને overwrite કરીને ફરી terminate કરે છે.
- યાદ રાખવાની યુક્તિ: એ જ tour, અલગ કામ.