Variables and Data types 1 Suppose you are writing a program
Variables and Data types.
1. Suppose you are writing a program to calculate a student\'s GPA A pseudocode version of such a program is the following: Start: Grade for COS 198 = 4.0; Number of hours for cos 198 = 3; Grade for MAT 126 = 3.5; Number of hours for MAT 126 = 4; Grade for ENG 101 = 4.0; Number of hours for ENG 101 = 3; Grade for PSY 100 = 3.5; Number of hours for PSY 100 = 3; Sum = (Grade for COS 198) * (Number of hours for cos 198) + (Grade for MAT 126) *(Number of hours for MAT 126) + (Grade for ENG 101) * (Number of hours for ENG 101) + (Grade for PSY 100) (Number of hours for PSY 100) Attempted = (Number of hours for COS 198) + (Number of hours for MAT 126) + (Number of hours for ENG 101) + (Number of hours for PSY 100); GPASum/Attempted; End What would you name the variables in the program if you were programming in: (a) A language that allows variable names that are a single letter followed by o, 1, or 2 digits? variable names beginning with a letter? with all variable names beginning with a letter? (b) A language with a 20-character limit on variable names, with all (c) A language with no limit on the length of variable names, and 2. Assume that the following statements are in sequence in a pro- gram (e.g., if statement 1 gives a variable a new value, the variableSolution
1
(a) Grades --- g1,g2,g3,g4
hours -h1,h2,h3,h4
sum ---- S
Attempted --A
GPA --- G
(b)
Grades --- gradeCOS,gradeMAT,gradeENG,gradePSY
hours -hrsCOS,hrsMAT,hrsENG,hrsPSY
sum ---- SUM
Attempted --Attempt
GPA --- SUM/Attempt
(c)
Grades --- gradeCOS198,gradeMAT126,gradeENG101,gradePSY100
hours -hrsCOS198,hrsMAT126,hrsENG101,hrsPSY100
sum ---- SUM
Attempted --Attemptedverage
GPA --- SUM/Attempted
2. A=B=C =0
A:=5 ; A=5
B:=A+C ; B= 5
C:= B+C; C =5
A:= 2+A ; A=2+5 =7
B:= A+B+C; B=7+5+5 = 17
3.
UNSIGNED INTEGERS
ADVANTAGES
Disadvantages
4
a) m+22
b)m+23
c)m+3
5
Address = BaseAddress + ((depthindex*colsize+colindex) * rowsize + rowindex) * ElementSize
Temp[40][30][20] = BaseAddress + ((depthindex*colsize+colindex) * rowsize + rowindex) * ElementSize
Now we have BaseAddress = m ,Elementsize = 4 bytes ,Colsize = 100 ,Rowsize = 100,Depthindex = 40
Colindex = 30 ,Rowindex = 20
Address = m + ( (40*100 + 30)*100 + 20 ) * 4 bytes
= m + ( (4030)*100 + 20 ) * 4 bytes
= m + (403020) * 4 bytes = m + 1612080

