tablica: array: dane_liczbowe

Similar documents
OBJECT ORIENTED PROGRAMMING

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Chapter 7 Array. Array. C++, How to Program

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

CS2141 Software Development using C/C++ C++ Basics

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

C++ For Science and Engineering Lecture 12

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

ONE DIMENSIONAL ARRAYS

Add Subtract Multiply Divide

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Program Organization and Comments

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

CMPS 221 Sample Final

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Array. Arrays. Declaring Arrays. Using Arrays

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

Arrays. Week 4. Assylbek Jumagaliyev

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Function a block of statements grouped together, to perform some specific task

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

C++ basics Getting started with, and Data Types.

Lab Instructor : Jean Lai

EE 109 Lab 8a Conversion Experience

Abstract Data Type (ADT) & ARRAYS ALGORITHMS & DATA STRUCTURES I COMP 221

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

Lab 3. Pointers Programming Lab (Using C) XU Silei

CHAPTER 4 FUNCTIONS. 4.1 Introduction

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

Review of Important Topics in CS1600. Functions Arrays C-strings

Unit 3 Decision making, Looping and Arrays

Fast Introduction to Object Oriented Programming and C++

CMSC 202 Midterm Exam 1 Fall 2015

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

Functions. Introduction :

C Programming Review CSC 4320/6320

REPETITION CONTROL STRUCTURE LOGO

Computers Programming Course 6. Iulian Năstac

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Computers Programming Course 11. Iulian Năstac

More Arrays. Last updated 2/6/19

Arrays and Strings. Arash Rafiey. September 12, 2017

ESC101N Fundamentals of Computing

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Here, type declares the base type of the array, which is the type of each element in the array size defines how many elements the array will hold

Programming. C++ Basics

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Chapter 01 Arrays Prepared By: Dr. Murad Magableh 2013

CSCE Practice Midterm. Data Types

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

a data type is Types

Getting started with C++ (Part 2)

Homework Assignment #1

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

C Functions. Object created and destroyed within its block auto: default for local variables

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Lesson 7. Reading and Writing a.k.a. Input and Output

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Baby Steps: Some Simple Programs in C, C++, and Java

AMCAT Automata Coding Sample Questions And Answers

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2013 CISC2200 Yanjun Li 1. Fall 2013 CISC2200 Yanjun Li 2

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. A single variable can only hold one value. Declared using [] operator:

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Computer Science & Engineering 150A Problem Solving Using Computers

Why VC++ instead of Dev C++?

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2017 CISC2200 Yanjun Li 1. Fall 2017 CISC2200 Yanjun Li 2

Linked List using a Sentinel

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CS222 Homework 4(Solution)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

MCA Semester 1. MC0061 Computer Programming C Language 4 Credits Assignment: Set 1 (40 Marks)

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Decision Making -Branching. Class Incharge: S. Sasirekha

C C C C++ 2 ( ) C C++ 4 C C

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Programming for Engineers Arrays

Input And Output of C++

Lecture 04 FUNCTIONS AND ARRAYS

Chapter 7 Functions. Now consider a more advanced example:

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as:

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

UEE1302 (1102) F10: Introduction to Computers and Programming

Fundamentals of Computer Programming Using C

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

Introduction to C++ Systems Programming

CS 31 Review Sheet. Tau Beta Pi - Boelter Basics 2. 2 Working with Decimals 2. 4 Operators 3. 6 Constants 3.

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

A First Program - Greeting.cpp

C and C++ I. Spring 2014 Carola Wenk

EC 413 Computer Organization

Chapter 2: Basic Elements of C++

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

C: How to Program. Week /Apr/23

Transcription:

ARRAYS IN C/C++ tablica: array: dane_liczbowe float dane_liczbowe[5]; dane_liczbowe[0]=12.5; dane_liczbowe[1]=-0.2; dane_liczbowe[2]= 8.0;... 12.5-0.2 8.0...... 0 1 2 3 4 indeksy/numery elementów positions / indexes of elements Array is a representation of data that allows grouping of several items of the same type and referring to them using common name. It is one of the most commonly used data types. The general form of array definition: type_of_element arrayname [ number_of_elements ] ; examples: float numerical_data[ 5 ]; // 5-element array of real numbers int tab[ 10 ]; // 10-element array of integers char text[ 255 ]; // 255-element array of characters double ( functions [ 20 ]) (double,double); // array of 20 function pointers M.Piasecki: Introduction To Programming 1 (L4) Arrays in C/C++

Notes on tables: An important feature of the table is a representation in the form of contiguous region in memory space and uniform distribution of subsequent components in immediate succession (directly one after the other) With such representation the computer can quickly calculate the position of an item in memory (based on its order number - index) and a significant reduction the code size, through the use of loops. Array elements are indexed starting from zero! C / C++ languages do not check the range (correctness) of indexes! This is often the cause of nasty (difficult to detect) errors. For example, assuming definition: float numerical_data [5]; instruction: numerical_data [5]=10.5; destroys / overwrites the memory after the last element of this array. The name of the array is, at the same time, the pointer the first element, ie. array_name == & array_name[0] Standard array does not store information about the number of its elements. Warning! function sizeof () does not return the number of elements. The definition of a multidimensional array: type array_name [size_1] [size_2] [size_3]... ; examples: char Rubik_Cube [ 3 ][ 3 ][ 3 ]; float matrix [ 5 ] [ 2 ]; // two-dimensional array of 5 rows, of 2 columns, 0, 0 0, 1 1, 0 1, 1 1, 2 2, 0 2, 1 3, 0 3, 1 4, 0 4, 1 matrix representation in computer memory 0, 0 0, 1 1, 0 1, 1 2, 0 2, 1 3, 0 3, 1 4, 0 4, 1 matrix[ 1 ][ 2 ] M.Piasecki: Introduction To Programming 2 (L4) Arrays in C/C++

The definition can be combined with initialization: int tab[ 10 ]; // definition without initialization int initialized_array [ 10 ] = 20, -3, 12, 1, 0, 7, -5, 100, 2, 5 ; char array_of_chars[ 5 ] = a, B, \n, 1, \0 ; float matrix_a[ 3 ][ 2 ] = 1,1, 3.5,7.0, -15,100 ; float matrix_b[ 3 ][ 2 ] = 1, 1, 3.5, 7.0, -15, 100 ; Subsequent "initializers" always are inserted to the subsequent "cells" (therefore inner curly braces can be omitted). If the list of initializers is shorter than the number of elements in the array, other elements are complemented with zeros or NULL pointers eg.: int tab[ 10 ] = 20, -3, 12, 1 ; is equivalent to: int tab[ 10 ] = 20, -3, 12, 1, 0, 0, 0, 0, 0, 0 ; definition: float matrix [ 3 ][ 2 ] = 1, 3.5,7.0 ; is equivalent to: float matrix[ 3 ][ 2 ] = 1,0, 3.5,7.0, 0,0 ; or: float macierz[ 3 ][ 2 ] = 1, 0, 3.5, 7.0, 0, 0 ; In C language, the initializers must be constant, whereas in C ++ the initializers can be both constant or variable. The use of constants to define the amount of array elements: int array [ 100 ] ; #define SIZE_C 100 int array [ SIZE_C ] ; const int SIZE_2 = 100 ; int array_2 [ SIZE_2 ] ; // the size defined directly // the definition of a constant using C-style // constant definition using C++ style for( int i=0 ; i < SIZE_2 ; i++ ) printf ( %d, array_2[ i ] ); // the example of using the size constant M.Piasecki: Introduction To Programming 3 (L4) Arrays in C/C++

Basic operations on the array elements #include <stdio.h> int main( ) const SIZE = 4 ; int Tab [ SIZE ] ; // direct assignment of values Tab[ 0 ] = 0 ; Tab[ 1 ] = 10 ; Tab[ 2 ] = - 20 ; Tab[ 3 ] = 3 ; // entering array content from the keyboard scanf( %d, &Tab[ 0 ] ) ; scanf( %d %d, &Tab[ 1 ], &Tab[ 2 ] ) ; printf( Enter a fourth element = ); scanf( %d, &Tab[ 3 ] ) ; // primitive summation of array elements long sum = Tab[0] + Tab[1] + Tab[2] + Tab[3] ; // displaying the contents of array elements printf( Tab[1] = %5d, Tab[0] ); printf( Tab[2] = %5d, Tab[1] ); printf( Tab[3] = %5d, Tab[2] ); printf( Tab[4] = %5d, Tab[3] ); // indirect indication of the element, using the auxiliary variable int i = 2 ; Tab[ i ] = 10; // is quivalent to: Tab[ 2 ] = 10; // entering the index of element, from the keyboard printf( Enter position of the element, whose value you want to load ); scanf( %d, &i ); printf( Enter a new value of Tab[ %d ] =, i ); scanf( %d, &Tab[ i ] ); printf( New value of Tab[ %d ] is equal %d, i, Tab[ i ] ); M.Piasecki: Introduction To Programming 4 (L4) Arrays in C/C++

Application of command "for" to automate operations on arrays #include <stdio.h> int main( ) #define LENGTH 10 float numbers[ LENGTH ]; // the definition of an array of real numbers int i ; // initialization of an array with successive even numbers: 0,2,4,6,... numbers[ i ] = 2 i ; // ---------------- loading the contents of array elements from the keyboard printf( Enter Number [%2d] =, i+1 ); scanf( %f, &numbers[ i ] ); // ----------------------------------- wyświetlenie zawartości elementów tablicy printf( Number[%2d] = %10.3f, i+1, numbers[ i ] ); //--------------------------------------------- summing the elements of an array float sum = 0 ; sum = sum + numbers[ i ]; // sum += numbers[ i ]; printf( The sum of the array elements is: %.2f, sum ); //---------------------- counting the number of elements with positive values int counter = 0 ; if(numbers[ i ] > 0 ) counter = counter + 1 ; // counter += 1; or counter ++; if( counter > 0 ) printf( Number of positive elements = %d, counter ); else printf( No positive values in the array ); M.Piasecki: Introduction To Programming 5 (L4) Arrays in C/C++

continued examples of array processing with loops for #include <iostream> using namespace std; int main( ) const int LENGTH = 10; int i, array[ LENGTH ]; //------- entering values from keyboard cout << Array[ << (i+1) << ] = ; cin >> array[ i ]; int counter=0; //--------------------------- counting of nonzero elements if( array[ i ] ) counter ++; int sum = 0; //------------------------------ an average of negative values counter =0; if( array[ i ] < 0 ) sum += array[ i ]; counter++; if( counter ) double average = (double)sum / counter; cout << endl << "Average of negatives = " << average ; else cout << endl << " There are no elements with negative values " ; int max = array[0]; // determine the maximum (value & position) int position = 0; for( i=1; i< LENGTH ; i++ ) if( max < array[ i ] ) max = array[ i ]; position = i ; cout << endl << "The biggest value is equal to: " << max; cout << endl << "Its position is: " << (position+1); M.Piasecki: Introduction To Programming 6 (L4) Processing arrays using loops

Examples of functions that operate on arrays #include <stdio.h> #include <conio.h> #define LENGTH 10 void readarray ( double array[ ] ) printf( Enter values of the array elements \n ); for( int i = 0 ; i < LENGTH ; i++ ) printf( Array[%2d] =, i+1 ); scanf( %lf, &array[ i ] ); //-------------------------------------------------------------------------------- readarray void displayarray( double array[ ] ) printf( The values of array elements are equal: \n ); for( int i = 0 ; i < LENGTH ; i++ ) printf( Array[%2d] = %f, i+1, array [ i ] ); printf( Press any key ); fflush(stdin); getch(); //----------------------------------------------------------------------------- displayarray void addarrays(double input_1[ ], double input_2[ ], double result[ ]) for( int i = 0 ; i < LENGTH ; i++ ) result[ i ] = input_1[ i ] + input_2[ i ] ; //-------------------------------------------------------------------------------- addarrays int main( void ) double A[ LENGTH ] ; double B[ LENGTH ], C[ LENGTH ] ; readarray( A ); displayarray( A ); readarray ( B ); addarrays( A, B, C ); displayarray ( C ); M.Piasecki: Introduction To Programming 7 (L4) Processing arrays using functions