Free Essay

C# Array

In:

Submitted By mhari27
Words 497
Pages 2
Background Information: * Array is a list of data items that all have the same data type and the same name * Each item in the array is called an element . * Each element has an index. Beginning index is 0

Example 1: Suppose we would like to store the scores of 4 students in a classroom
// create an array called scores whose size is 4 and stores numbers of type double double[ ] scores= new double[4];
//Assign values to the elements of the array . scores [0]=90; scores[1]= 95; scores [2]=70; scores[3]= 60; 90 | 95 | 70 | 60 | scores [0] scores [1] scores [2] scores [3]

Example 2:
You can also assign values to array elements using an Initializer list:
// create an double array called scores and fill it with the 4 test scores 90, 95, 70, 60 double[ ] scores = {90, 95, 70, 60}; 90 | 95 | 70 | 60 | scores [0] scores [1] scores [2] scores [3] * Write code to display the value of the third element in a label: lblOutput.Text = scores[2].ToString(); * write a line of code to get the length of the array

int size = scores.Length; // size is 4

Exercise 1 Code | | string[] letters = {“A”,”B”};int size = letters.Length; | What is the value of size ?What is stored in letters[1] ? |

Exercise 2 Code | | int[] numbers = {10,20,30};int size = numbers.Length; | What is the value of size ?What is the value of : numbers[0] + numbers[1] + numbers[2] ? |

Exercise 3 Code | After code executes: | int[] numbers = {10,20,50};int size = numbers.Length;numbers[2]= numbers[0]+ numbers[1]; | What is stored in numbers[2] ? |

Exercise 4 Write code that initializes all the elements in an integer array grades (of size 10) to zero .i.e. need to fill the elements of the array with the value zero | int[] grades = new grades[10];// create the arrayfor (int i = 0; i <= 9; i++) { grades[?]= 0; } |

Task 1 Create an appliacation and design the form as follows:When the button is clicked you should declare and initialize a grades array to the integer values : 95, 60, 80. The numbers in the array are then displayed in a listbox lstOutput in this format:956080 | solution |

Task 2:Create an application and design the form as follows:When the button is clicked you should declare and initialize a myScores array to the integer values : 100, 90, 80, 70You should then calculate and display the sum and average of the numbers in the in labels | } |

Task 3
Write a program that allows the user to enter up to 2 orders .The program should store the order totals in an array. When the Calculate button is clicked the total is calculated as 100 * quantity entered.
If the user attempts to enter more than 2 orders, an error message will alert the user.
When the Statistics button is clicked the sum of the totals in the array and the average are displayed in a MessageBox.

|

|

o

Similar Documents

Free Essay

Tutorial on Pointers and Arrays in C

...A Tutorial on Pointers and Arrays in C A TUTORIAL ON POINTERS AND ARRAYS IN C by Ted Jensen Version 1.1 (HTML version) July 1998 This material is hereby placed in the public domain Available in various formats via http://www.netcom.com/~tjensen/ptr/cpoint.htm TABLE OF CONTENTS Preface Introduction Chapter 1: What is a Pointer? Chapter 2: Pointer Types and Arrays. Chapter 3: Pointers and Strings Chapter 4: More on Strings Chapter 5: Pointers and Structures Chapter 6: More on Strings and Arrays of Strings Chapter 7: More on Multi-Dimensional Arrays Chapter 8: Pointers to Arrays Chapter 9: Pointers and Dynamic Allocation of Memory Chapter 10: Pointers to Functions file:///E|/My%20eBooks/_ESSENTIALS_/A%20Tutorial%...orial%20on%20Pointers%20and%20Arrays%20in%20C.htm (1 of 2)3/18/2007 12:09:49 AM A Tutorial on Pointers and Arrays in C Epilog file:///E|/My%20eBooks/_ESSENTIALS_/A%20Tutorial%...orial%20on%20Pointers%20and%20Arrays%20in%20C.htm (2 of 2)3/18/2007 12:09:49 AM Preface PREFACE This document is intended to introduce pointers to beginning programmers in the C programming language. Over several years of reading and contributing to various conferences on C including those on the FidoNet and UseNet, I have noted a large number of newcomers to C appear to have a difficult time in grasping the fundamentals of pointers. I therefore undertook the task of trying to explain them in plain language with lots of examples. The first version of this document was...

Words: 9878 - Pages: 40

Free Essay

Vba Intro

...Subroutine . . . 4.4 Functions can call functions . . . . . . . . . . . 4.5 Illegal Function Names . . . . . . . . . . . . . . 4.6 Differences Between Functions and Subroutines 3 3 4 5 5 7 7 8 9 9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ∗ Copyright c 1995-2000 Robert L. McDonald. Thanks to Jim Dana for asking stimulating questions about VBA. † Finance Dept, Kellogg School, Northwestern University, 2001 Sheridan Rd., Evanston, IL 60208, tel: 847-491-8344, fax: 847-491-5719, E-mail: r-mcdonald@northwestern.edu. CONTENTS 2 5 Storing and Retrieving Variables in a Worksheet 5.1 Using a named range to read and write numbers from spreadsheet . . . . . . . . . . . . . . . . . . . . . . . . . 5.2 Reading and Writing to Cells Which are not Named. . . 5.3 Using the “Cells” Function to Read and Write to Cells. 10 the . . . . . . . . . 11 12 13 6 Using Excel Functions 13 6.1 Using VBA to compute the Black-Scholes formula . . . . . . 13 6.2 The Object Browser . . . . . . . . . . . . . . . . . . . . . . . 15 7 Checking for Conditions 16 8 Arrays 17 8.1 Defining Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . 18 9 Iterating 19 9.1 A simple for loop . . . . . . . . . . . . . . . . . . . . . . . . . 20 9.2 Creating a binomial tree . . . . . . . . . . . . . . . . . . . . . 20 9.3 Other...

Words: 10883 - Pages: 44

Free Essay

Matlab & Ss

...language itself bears some similarities with ANSI C and FORTRAN. MATLAB works with three types of windows on your computer screen. These are the Command window, the Figure window and the Editor window. The Figure window only pops up whenever you plot something. The Editor window is used for writing and editing MATLAB programs (called M-files) and can be invoked in Windows from the pull-down menu after selecting File | New | M-file. In UNIX, the Editor window pops up when you type in the command window: edit filename (‘filename’ is the name of the file you want to create). The command window is the main window in which you communicate with the MATLAB interpreter. The MATLAB interpreter displays a command >> indicating that it is ready to accept commands from you. • View the MATLAB introduction by typing >> intro at the MATLAB prompt. This short introduction will demonstrate some basic MATLAB commands. • Explore MATLAB’s help capability by trying the following: >> help >> help plot >> help ops >> help arith • Type demo and explore some of the demos of MATLAB commands. • You can use the command window as a calculator, or you can use it to call other MATLAB programs (M-files). Say you want to evaluate the expression [pic], where a=1.2, b=2.3, c=4.5 and d=4. Then in the command window, type: >> a = 1.2; >> b=2.3; >> c=4.5; >> d=4; >> a^3 + sqrt(b*d) - 4*c ans = -13.2388 Note the...

Words: 2151 - Pages: 9

Free Essay

Ecet 370 Hw1

...======================================== DeVry College of New York ECET 370 HW #1: Dynamic 2D-array ------------------------------------------------- Objective: Write a program to calculate students’ average test scores and their grades. You may assume the following file input data: Johnson 85 83 77 91 76 Aniston 80 90 95 93 48 Cooper 78 81 11 90 73 Gupta 92 83 30 69 87 Blair 23 45 96 38 59 Clark 60 85 45 39 67 Kennedy 77 31 52 74 83 Bronson 93 94 89 77 97 Sunny 79 85 28 93 82 Smith 85 72 49 75 63 Use three arrays: a one-dimensional array to store the students’ names, a (parallel) two-dimensional array to store the test scores, and a parallel one dimensional array to store grades. Your program must contain at least the following functions: a function to read and store data into two arrays, a function to calculate the average test score and grade, and a function to output the results. Have your program also output the class average. Tools required: PC & Visual studio software Code: #include <iostream> #include <fstream> #include <sstream> #include <string> using namespace std; bool readStudentData(ifstream &inFile, string &name, int &score1, int &score2, int &score3, int &score4, int &score5) { string line; if (getline(inFile, line)) { string word; istringstream iss(line, istringstream::in); int count = 0; try { while (iss >> word) { //cout...

Words: 673 - Pages: 3

Premium Essay

Logic and Design

...Programming Logic and Design, 6th Edition Chapter 6 Exercises 1. a. Design the logic for a program that allows a user to enter 10 numbers, then displays them in the reverse order of their entry. Answer: A sample solution follows Flowchart: Pseudocode: start Declarations num index num SIZE = 10 num numbers[SIZE] = 0,0,0,0,0,0,0,0,0,0 getReady() while index < SIZE getNumbers() endwhile finishUp() stop getReady() index = 0 return getNumbers() output “Enter a number for position ”, index input numbers[index] index = index + 1 return finishUp() output “The numbers in reverse order are: ” while index > 0 index = index – 1 output numbers[index] endwhile return b. Modify the reverse-display program so that the user can enter up to 10 numbers until a sentinel value is entered. Answer: A sample solution follows Flowchart: Pseudocode: start Declarations num index num SIZE = 10 num numbers[SIZE] = 0,0,0,0,0,0,0,0,0,0 string CONTINUE = “Y” string moreNumbers = CONTINUE getReady() while index < SIZE AND moreNumbers equal to CONTINUE getNumbers() endwhile finishUp() stop getReady() index = 0 output “Do you want to enter a number? (Y/N)” input moreNumbers return getNumbers() output “Enter a number for position ”, index input numbers[index] index = index...

Words: 4366 - Pages: 18

Free Essay

Java Array

...Java provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. This tutorial introduces how to declare array variables, create arrays, and process arrays using indexed variables. Declaring Array Variables: To use an array in a program, you must declare a variable to reference the array, and you must specify the type of array the variable can reference. Here is the syntax for declaring an array variable: ------------------------------------------------- dataType[] arrayRefVar; // preferred way. ------------------------------------------------- ------------------------------------------------- or ------------------------------------------------- ------------------------------------------------- dataType arrayRefVar[]; // works but not preferred way. Note: The style dataType[] arrayRefVar is preferred. The style dataType arrayRefVar[] comes from the C/C++ language and was adopted in Java to accommodate C/C++ programmers. Example: The following code snippets are...

Words: 1665 - Pages: 7

Free Essay

Mis505 Work

..._____. a. element in an array b. alternate name for an array c. number that represents the highest value stored within an array d. number that indicates the position of a particular item in an array 2. Each variable in an array must have the same _____ as the others. a. data type b. subscript c. value d. memory location 3. Each data item in an array is called a(n) _____. a. data type b. subscript c. component d. element 4. The subscripts of any array are always _____. a. integers b. fractions c. characters d. strings of characters 5. Suppose you have an array named number, and two of its elements are number[1] and number[4]. You know that _____. a. the two elements hold the same value b. the array holds exactly four elements c. there are exactly two elements between those two elements d. the two elements are at the same memory location 6. Suppose you want to write a program that inputs customer data and displays a summary of the number of customers who owe more than $1,000 each, in each of 12 sales regions. Customer data variables include name, zipCode, balanceDue, and regionNumber. At some point during record processing, you would add 1 to an array element whose subscript would be represented by _____. a. name b. zipCode c. balanceDue d. regionNumber 7. The most useful type of subscript for manipulating arrays is a _____. a. numeric constant b. variable c. character d. filename 8. A program contains a seven-element array that holds the names of...

Words: 843 - Pages: 4

Free Essay

C++ Chap1-8 Quiz Summary

... | c. Operators  | | | d. Program ops  | | | e. None of the above  | | Correct Marks for this submission: 1/1. Question2 Marks: 1 Programmer-defined names of memory locations that may hold data are: Choose one answer. | a. Operators  | | | b. Variables  | | | c. Syntax  | | | d. Operands  | | | e. None of the above.  | | Correct Marks for this submission: 1/1. Question3 Marks: 1 Which of the following best describes an operator? Choose one answer. | a. An operator is a rule that must be followed when constructing a program.  | | | b. An operator allows you to perform operations on one or more pieces of data.  | | | c. An operator marks the beginning or ending of a statement, or is used to separate items in a list.  | | | d. An operator is a word that has a special meaning.  | | | e. An operator is a symbolic name that refers to a variable.  | | Correct Marks for this submission: 1/1. Question4 Marks: 1 What does the term hardware refer to? Choose one answer. | a. The relative difficulty of programming  | | | b. The physical components that a computer is made of  | | | c. The way a computer's storage space is organized  | | | d. The logical flow of instructions  | | | e. None of the above.  | | Correct Marks for this submission: 1/1. Question5 Marks: 1 The computer's main memory is commonly known as: Choose one answer. | a. The hard disk  | | | b. The floppy disk  | | | c. RAM ...

Words: 10107 - Pages: 41

Free Essay

Join

...or values, the array can hold. a.|the new operator| b.|the array’s size declarator| c.|the array’s data type| d.|the version of Java| ANS: B 2. What does the following statement do? double[] array1 = new double[10]; a.|Declares array1 to be a reference to an array of double values| b.|Creates an instance of an array of 10 double values| c.|Will allow valid subscripts in the range of 0 - 9| d.|All of the above| ANS: D 3. It is common practice to use a ____________ variable as a size declarator. a.|static| b.|reference| c.|final| d.|boolean| ANS: C 4. What do you call the number that is used as an index to pinpoint a specific element within an array? a.|subscript| b.|global unique identifier| c.|element| d.|argument| ANS: A 5. Subscript numbering always starts at what value? a.|0| b.|1| c.|-1| d.|None of the above| ANS: A 6. By default, Java initializes array elements with what value? a.|0| b.|100| c.|1| d.|-1| ANS: A 7. What will be the value of x[8] after the following code has been executed? final int SUB = 12; int[] x = new int[SUB]; int y = 100; for(int i = 0; i < SUB; i++) { x[i] = y; y += 10; } a.|170| b.|180| c.|190| d.|200| ANS: B 8. Java performs ____________, which means that it does not allow a statement to use a subscript that is outside the range of valid subscripts for the array. a.|active array sequencing| b.|array bounds checking| c.|scope resolution...

Words: 1531 - Pages: 7

Premium Essay

Comp 230 Week 6 Lab Doc

...VBScript IP File Lab Objectives In this lab, students will complete the following objectives. * Create a VBScript program using NotePad++. * Write a two-dimensional array of IP addresses to a text file. * Read the IP Addresses text file into a script. * Append new Room/PC/IP address data to the text file. * Use the object Scripting.FileSystemObject. Lab Diagram During your session you will have access to the following lab configuration. Connecting to your lab For this lab, we will only need to connect to Vlab-PC1. * Vlab-PC1 To start simply click on the named Workstation from the device list (located on the left hand side of the screen) and click Power on in the tools bar. In some cases the devices may power on automatically. During the boot up process an activity indicator will be displayed in the name tab. * Black—Powered Off * Orange—Working on your request * Green—Ready to access If the remote console is not displayed automatically in the main window (or popup) click the Connect icon located in the tools bar to start your session. If the remote console does not appear please try the following option. * Switch between the HTML 5 and Java client versions in the tools bar. In the event this does not resolve your connectivity problems, please visit our Help/Support pages for additional resolution options. Task 1: Create the IP_FileWrite.vbs Program Note: All captures must be text only—DO NOT capture the NotePad++...

Words: 2335 - Pages: 10

Free Essay

Indi Assign

... Programming Development: Personal Learning Management The part of the program I believe to need an array would be the entire program. The Subject, the Classes, the weeks, the keywords, and the notes. I am still unsure as to how the arrays work, I have been trying to practice them, but there is not much in the way of instruction on how to use them. I know it can be a list or a table, which works well with the program. For example, the program can be a table starting from the subject, breaking down into the classes, which break down into the weeks that break down into the keywords and the notes. In my opinion, the only way the program could be put together in its simplest form would be in an array. Using an array can reduce the number of variable names needed in a program because a single array can be used instead of a collection of simple variables to store related data (Venit, Stewart & Drake, Elizabeth; Prelude to Programming Concepts and Design; Chapter 6- Arrays: Lists and Tables.). So using an array is the best way to simplify my entire program. The best part of the array is being able to set up the search for the index of keywords. It will make being able to type in the keyword and pull up the table containing that keyword much easier. For this I will have to set it up for a binary search. The following pseudocode is updated to include the arrays and the binary search: Pseudocode Display “Student Notebook” Display “Subject...................

Words: 807 - Pages: 4

Free Essay

Student

...An Introduction to R Notes on R: A Programming Environment for Data Analysis and Graphics Version 3.2.0 (2015-04-16) W. N. Venables, D. M. Smith and the R Core Team This manual Copyright c Copyright c Copyright c Copyright c Copyright c is for R, version 3.2.0 (2015-04-16). 1990 W. N. Venables 1992 W. N. Venables & D. M. Smith 1997 R. Gentleman & R. Ihaka 1997, 1998 M. Maechler 1999–2015 R Core Team Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies. Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one. Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions, except that this permission notice may be stated in a translation approved by the R Core Team. i Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 Introduction and preliminaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 1.10 1.11 2 Intrinsic attributes: mode and length . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

Words: 8172 - Pages: 33

Free Essay

Student Expert

...Program 1: Write a program to copy the contents of one array into another in the reverse order. Code: #include<stdio.h> int main() { int arry1[10],arry2[10]={0},i,j; printf("Enter the elements of array: \n"); for(i=0;i<10;i++) { printf("Enter element %d:",i+1); scanf("%d",&arry1[i]); } i=0; for(j=9;j>=0;j--) { arry2[j]=arry1[i]; i+=1; } printf("Array elements in reverse order are: \n"); for(i=0;i<10;i++) { printf("element %d:%d \n",i+1,arry2[i]); } return(0); } Output: Program 2: If an array arr contains n elements, then write a program to check if arr[0] = arr[n-1], arr[1] = arr[n-2] and so on. Code: #include<stdio.h> int main() { int arry1[10],i,j,k=0; printf("Enter the 10 elements of array: \n"); for(i=0;i<10;i++) { printf("Enter element %d:",i+1); scanf("%d",&arry1[i]); } j=9; for(i=0;i<5;i++) { if(arry1[i]==arry1[j]) { printf("Elemets %d and %d are same \n", i+1,j+1); k=1; } j--; } if(k==0) { printf("No match found\n"); } return(0); } Output: Program 3: Find the smallest number in an array using pointers. Code: #include<stdio.h> int main() { int arry1[10],i,*j,k=10000; printf("Enter the 10 elements of array: \n"); for(i=0;i<10;i++) { printf("Enter element %d:",i+1); scanf("%d",&arry1[i]); } j=&arry1[0]; for(i=0;i<9;i++) ...

Words: 1358 - Pages: 6

Free Essay

Problem Solving

...Declaring constants in Pascal A constant is a value that is given a name. The constant can be used anywhere in a program and will always have the same value. Constants are declared in the declaration section of the program, that is the section before begin using the keyword, Const. Value Constant name Name: data type= value; Data type A command is an instruction that is a single word. Eg. READ For example to declare a constant to store a value for tax: Const Tax: real=.15; TEST YOUR UNDERSTANDING Initialize the following constants in Pascal: i. ii. The number of seconds in a minute. iii. The temperature at which water boils iv. The number of months in a year. v. The discount amount for 5%. Writing data to the screen (WRITE and WRITELN) Pascal uses write and writeln to display information on the computer screen. These statements are useful when prompting user for data and displaying information of a processed result to the screen. * Writeln is also useful when a space is needed in the program. You can create a space by typing: WRITELN; * Writeln can also be used to display a string and a variable: WRITELN (‘the sum is’, sum); Before you read, you must write. The write is used when we want the data to be displayed on the same line, while writeln is used when we want to accept data in a new line. Place the information that you are writing to the screen in brackets and single quotes. Example: Write (‘enter your...

Words: 3441 - Pages: 14

Free Essay

Jesus

...javax.microedition.lcdui Class TextField java.lang.Object | +--javax.microedition.lcdui.Item | +--javax.microedition.lcdui.TextField public class TextField extends Item A TextField is an editable text component that may be placed into a Form. It can be given a piece of text that is used as the initial value. A TextField has a maximum size, which is the maximum number of characters that can be stored in the object at any time (its capacity). This limit is enforced when the TextField instance is constructed, when the user is editing text within the TextField, as well as when the application program calls methods on the TextField that modify its contents. The maximum size is the maximum stored capacity and is unrelated to the number of characters that may be displayed at any given time. The number of characters displayed and their arrangement into rows and columns are determined by the device. The implementation may place a boundary on the maximum size, and the maximum size actually assigned may be smaller than the application had requested. The value actually assigned will be reflected in the value returned by getMaxSize(). A defensively-written application should compare this value to the maximum size requested and be prepared to handle cases where they differ. Input Constraints The TextField shares the concept of input constraints with the TextBox object. The different constraints allow the application to request that the user's input be restricted in a variety of ways...

Words: 3677 - Pages: 15