Está en la página 1de 14

U1246484

A03330984

ENGNIEERING COMPUTING

LAB 6: ARRAY
INTRODUCTION This lab was mainly design in the prospective of Array. An array is really a collection of exact identical type factors beneath the very similar varied symbol documented through index number. Arrays are usually popular inside of development for various purposes for instance selecting, seeking and also and so forth. Arrays enable you to store a group of info of a solitary kind. Assortments tend to be effective as well as useful for carrying out operations. You may use them to shop a set of large results in a video game, a couple sizing chart structure, or even retailer the coordinates of your ay as a spread she matrix regarding linear algebra calculations. Noted be a single column, whereas a multiple dimensional array would span out n columns by rows. AIMS Recognise when to use decision logic Recognise when to use different repetition structures OBJECTIVES This lab was mainly designed to reinforce programming concept of the following and to practice them. Logical expression Reading and writing one and two dimensional array

Nested if and switch statement

ENGINEERING COMPUTING

Page 1

U1246484 PART A EXERCISE 1 Question (a) below shows the compile program Program listing
// WILSON ROBERT TAMBO // UEL ID U1246484 #include<iostream.h> void main() { double Y[5];char Ch[10]={'Y','E','L','L','O','W'}; float Z[5]={6.5};int X[5]={4,8,10,3}; int M[3,3]={0,1,2,3,4,5,6,7,8},i,j; for(i=1;i<8;i=i+1) {cout<<Ch[i]<<"\t"<<Y[i]<<"\t"<<X[i]<<"\t"<<Z[i]<<endl;} Y[0]=12.5; X[5]=(Y[0]+13)/Z[i-3]; cout<<X[5]; for(i=0;i<4;i=i+1){ if(max<x[i]){max=X[i];}} cout<<max; for(i=0;i<4;i=i+1) {for(j=0;j<4;j=j+1)cout<<M[i,j]<<"\t";} cout<<endl;} }

A03330984

1>------ Build started: Project: best, Configuration: Debug Win32 -----1>Build started 8/3/2013 8:54:06 PM. 1>InitializeBuildStatus: 1> Creating "Debug\best.unsuccessfulbuild" because "AlwaysCreate" was specified. 1>ClCompile: 1> All outputs are up-to-date. 1> best.cpp 1>c:\users\wilson\documents\visual studio 2010\projects\robert\best\best\best.cpp(7): fatal error C1083: Cannot open include file: 'iostream.h': No such file or directory 1> 1>Build FAILED. 1> 1>Time Elapsed 00:00:01.24 ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

ENGINEERING COMPUTING

Page 2

U1246484 Question (b) below shows the fix error of the program Program listing
// WILSON ROBERT TAMBO // UEL ID U1246484

A03330984

#include <iostream> using namespace std; void main() { double y[5];char Ch[10]={'Y','E','L','L','O','W'}; float z[5]={6.5};int x[5]={4,8,10,3}; int m[3][3]={0,1,2,3,4,5,6,7,8},i,j,max=0; for(i=1;i<8;i=i+1) { cout<<Ch[i]<<"\t"<<y[i]<<"\t"<<x[i]<<"\t"<<z[i]<<endl; } y[0]=12.5; x[5]=(y[0]+13)/z[i-3]; cout<<x[5]; for(i=0;i<4;i=i+1) { if(max<x[i]) { max=x[i]; } } cout<<max; for(i=0;i<4;i=i+1) { for(j=0;j<4;j=j+1) cout<<m[i,j]<<"\t"; } cout<<endl; system("color f0"); system("pause"); }

'best.exe': Loaded 'C:\Users\Wilson\Documents\Visual Studio 2010\Projects\robert\best\Debug\best.exe', Symbols loaded. 'best.exe': Loaded 'C:\WINDOWS\SysWOW64\ntdll.dll', Cannot find or open the PDB file 'best.exe': Loaded 'C:\Program Files\AVAST Software\Avast\snxhk.dll', Cannot find or open the PDB file 'best.exe': Loaded 'C:\WINDOWS\SysWOW64\kernel32.dll', Cannot find or open the PDB file 'best.exe': Loaded 'C:\WINDOWS\SysWOW64\KernelBase.dll', Cannot find or open the PDB file 'best.exe': Loaded 'C:\WINDOWS\SysWOW64\msvcp100d.dll', Symbols loaded. 'best.exe': Loaded 'C:\WINDOWS\SysWOW64\msvcr100d.dll', Symbols loaded. 'best.exe': Loaded 'C:\WINDOWS\SysWOW64\apphelp.dll', Cannot find or open the PDB file 'best.exe': Loaded 'ImageAtBase0x4a6c0000', Loading disabled by Include/Exclude setting. 'best.exe': Unloaded 'ImageAtBase0x4a6c0000'

ENGINEERING COMPUTING

Page 3

U1246484

A03330984

'best.exe': Loaded 'ImageAtBase0x4a150000', Loading disabled by Include/Exclude setting. 'best.exe': Unloaded 'ImageAtBase0x4a150000' The thread 'Win32 Thread' (0x15cc) has exited with code -1073741510 (0xc000013a). The program '[3532] best.exe: Native' has exited with code -1073741510 (0xc000013a).

Below shows the output result of the program

ENGINEERING COMPUTING

Page 4

U1246484 EXERCISE 2

A03330984

Question (a) write the statement of the program which declaring character types array, give the brief explanation regarding initialization and number of element of the array. Below given the explanation Array elements are the scalar data that define an array. Each and every component gets the sort, sort guidelines, and purpose and also parameter, and also focus on characteristics from its father or mother assortment. The actual pointer characteristic isn't passed down. Also Arrays must be declared before they can be used in the program. Standard array declaration is as Type variable name [lengthofarray]. Question (b) write part of the program which search for the biggest number in array X Program listing // WILSON ROBERT TAMBO //UEL ID U1246484 # #include <iostream> using namespace std; void main() { int M[5] = {4,8,10,3}; int biggest= 0; for(int i=0;i<5;i++) { if(M[i]>biggest) biggest=M[i]; } cout <<"Please key in the biggest number is: " << biggest<< endl<<endl; system("color f0"); system ("pause"); }

ENGINEERING COMPUTING

Page 5

U1246484 Below shows the output result of the program

A03330984

Question (c) change the values of array X to 2, 6, 9, 7 and run the program. Program listing // WILSON ROBERT TAMBO //UEL ID U1246484 #include <iostream> using namespace std; void main() { int M[5] = {2,6,9,7}; int biggest= 0; for(int i=0;i<4;i++) { if(M[i]>biggest) biggest=M[i]; } cout <<"Please key in the biggest number is: " << biggest<< endl<<endl; system("color f0"); system ("pause"); } Below shows the output result of the program

ENGINEERING COMPUTING

Page 6

U1246484

A03330984

Question (d) Change the value of array M so that the program display unit matrix. Program listing //WILSON ROBERT TAMBO // UEL ID U 1246484 #include<iostream> using namespace std; void main () { int m[3][3]={1,0,0,0,1,0,0,0,1},i,j; for(i=0;i<4;i=i+1) { for(j=0;j<4;j=j+1) {cout<< m[i][j]<<"\t";} cout<<endl;} system("color f0"); system("pause"); }

Below shows the output result of the program

ENGINEERING COMPUTING

Page 7

U1246484

A03330984

Question (e) modify the program to find the smallest number of X Program listing // WILSON ROBERT TAMBO // UEL ID U1246484 #include<iostream> using namespace std; void main () { int i; double smallest=x0,biggest=0,x[5]={2,6,9,7}; for(i=0;i<4;i=i+1) {if(biggest<x[i]) {biggest=x[i];}} smallest=biggest; for(i=0;i<4;i=i+1) {if(x[i]<=smallest) {smallest=x[i];}} cout<<"The smallet number of X is :"<<smallest<<endl; system("color f0"); system("pause"); }

Below shows the output result of the program

ENGINEERING COMPUTING

Page 8

U1246484 PART B EXERECIS 3

A03330984

Question. Write a program that will prompt user enter 12 numbers. Your program will display the biggest number, the smallest number and average of the numbers given. Program listing // WILSON ROBERT TAMBO // UEL ID U1246484 #include<iostream> using namespace std; void main () { float x[12],sum=0,biggest=0,smallest=0,average; int i; cout<<"please key in 12 numbers to display smallest\n"; cout<<"please key in the biggest and the average numbers\n\n"; for(i=1;i<13;i=i+1) {cout<<i<<"pease key in the number\n"; cin>>x[i];sum=sum+x[i];} average=sum/12; for(i=1;i<13;i=i+1){if(biggest<x[i]){biggest=x[i];}} smallest=biggest; for(i=1;i<13;i=i+1){if(x[i]<smallest){smallest=x[i];}} cout<<endl; cout<<"The biggest number is:"<<biggest<<endl<<endl; cout<<"The smallest number is:"<<smallest<<endl<<endl; cout<<"The average number is:"<<average<<endl<<endl; system("color f0"); system("pause"); } Below show the output result of the program

ENGINEERING COMPUTING

Page 9

U1246484 EXERCIS 4

A03330984

Question. Write a program to search a given number in an array of 25 integer, Display proper message if the number is the array or not. Program listing // WILSON ROBERT TAMBO // UEL ID U1246484 #include<iostream> using namespace std; void main() { int M[25]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}; int i,x,found=0; cout<<"please key in the number between 0-25\n"; cout<<"please key in the number\n"; cin>>x; for(i=0;i<25;i=i+1) { if(x==M[i]) { found=1;} } if(found==1) cout<<"The number is in array\n"; if(found==0) cout<<"The number is not in the array\n"; cout<<endl; system("color f0"); system("pause"); }

ENGINEERING COMPUTING

Page 10

U1246484 Below show the output result of the program

A03330984

ENGINEERING COMPUTING

Page 11

U1246484 EXERCISE 5

A03330984

Question. Write a program to get value of 3x3 matrixes interactively from the user Program listing
#include<iostream>

using namespace std; void main () { int m[3][3]; int b,c; cout<<"please key in the vaule of 3 X 3 matrix\n"; for(b=0;b<3;b=b+1) {cout<<"please key in the value"<<b<<"row values:="<<endl; for(c=0;c<3;c=c+1) {cout<<"="; cin>>m[b][c];}} cout<<"\n\n"<<endl; for(b=0;b<3;b=b+1) {for(c=0;c<3;c=c+1) {cout<<m[b][c]<<"\t";} cout<<endl;} system("color f0"); system("pause"); } Below shows the output result of the program

ENGINEERING COMPUTING

Page 12

U1246484 RESULT DISCUSSION

A03330984

This lab was developed with the help of array and loops to design the complete system. In exercise 1 after compiling and fix the error the program display the output result.in exercise 2b the user was ask to write part of the program which search for the biggest number in array X and the program display the biggest number in array x which 10 with the help of for loop. Also the same in exercise 2c when change the values of array X to 2, 6, 9, 7 and run the program the output was different and the biggest number was 9 with the help of for loop.in exercise 3 the Write a program that will prompt user enter 12 numbers. Your program will display the biggest number, the smallest number and average of the numbers given. And after developed the program with the help of for-loop and the user key in 1 to 12 the program display the biggest number 12 and the smallest number 1 and the average number 6.5And in exercise 5 Write a program to get value of 3x3 matrixes interactively from the user. And with the help of for-loop to develop the program and the program display 3x3 matrixes.

CONCLUSION This lab was mainly design in the prospective of Array. An array is really a collection of exact identical type factors beneath the very similar varied symbol documented through index number. Arrays are usually popular inside of development for various purposes for instance selecting, seeking and also and so forth.In this lab the array was used in different way to find different value, and also the statement of the program which declaring character types array was also give which stated that Array elements are the scalar data that define an array. Each and every component gets the sort, sort guidelines, and purpose and also parameter, and also focus on characteristics from its father or mother assortment. The actual pointer characteristic isn't passed down. Also Arrays must be declared before they can be used in the program. Standard array declaration is as Type variable name [lengthofarray].

ENGINEERING COMPUTING

Page 13

U1246484

A03330984

REFERENCE

Z, D. (1998) An troduction to programming with c++.

ENGINEERING COMPUTING

Page 14

También podría gustarte