Write an array declaration in QBasic to create an array that can store marks of 5 students.
Answers
Answer:
An array is a structured type that is useful for storing many individual values that are all of the same type. For example, consider a situation where you have to find the averages for sixty students in a class. Instead of creating sixty individual variables to hold data for each student like we would have to do using simple variables, we could use one single data array to store the sixty individual values as a group listing. Formally, an array is an ordered group of related data items (of the same type) having a common variable name. An array is primarily used when groups of data items must be stored and manipulated efficiently. An array is given a name and also a specified number of data items that it will contain; each individual item is referred to as an element. Each element is given a unique storage location in the array and a subscript or index value is used to identify the position of a particular element. The subscript, which may be an expression or simple variable, is enclosed in parantheses after the array name. Unless specified otherwise, the first element in an array is always given an index value of 1 and the index is incremented for each element thereafter. Up to this point in this tutorial, we have only dealt with unsubscripted variables, which are simple variables that are only capable of storing one value. An array is called a subscripted variable becuase when we need to access a certain element, we must use a subscript to point to that element so we can differentiate it from the rest.
In order to declare an array, we must use a DIM statement which plays the role of reserving internal memory space for an array of any desired size. The form of declaring a one-dimensional array is as follows:
Explanation:
REM outputting information contained in a 1D array DIM customers$(1 TO 50), balances(1 TO 50) count = 1 INPUT "Enter the name of the customer: "; customer$ INPUT "Enter the customer's balance (-999 to quit): "; balance DO WHILE (count < 50) AND (balance <> -999) customers$(count) = customer$ balances(count) = balance count = count + 1 INPUT "Enter the name of the customer: "; customer$ INPUT "Enter the customer's balance (-999 to quit): "; balance LOOP count = count - 1 sizeOfArray = count FOR counter = 1 TO sizeOfArray PRINT "Customer Name: "; customers$(counter) PRINT "Customer Balance: "; balances(counter) NEXT counter REM outputting information contained in a 2D array DIM stuData(1 TO 20, 1 TO 10) DIM totalPts(1 TO 20) FOR row = 1 TO 20 FOR col = 1 TO 10 PRINT "Enter test score"; col; "for student"; row; ": " INPUT stuData(row, col) totalPts(row) = totalPts(row) + stuData(row, col) NEXT col NEXT row FOR row = 1 TO 20 FOR col = 1 TO 10 PRINT "Stu Test Scores: "; stuData(row, col); NEXT col PRINT NEXT row