473,411 Members | 2,184 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Very New to C++ and need help

4
Hello,

Like I've said C++ is very new to me and I have some questions...

1. #include <iostream>
2. using namespae std;
3. const int MAXSIZE = 10;
4. main(){
5. int tbl[ MAXSIZE ], n;
6. n = 0;
7. while( ( n < MAXSIZE ) && ( cin >> tbl[ n ] ) ) { n++; }
8. cout << “FIRST ELEMENT ” << tbl[ 0 ] << endl;
9. cout << “MIDDLE ELEMENT ” << tbl[ n / 2 ] << endl;
10. cout << “LAST ELEMENT ” << tbl[ n – 1 ] << endl;
11. return 0;
12. }//MAIN

please correct my terminology if it's wrong.

I'm having a hard time understanding this program...I dont even know where to begin. I'm not sure why in line seven I understand the first part of the loop. But if tbl is declared with the varible MAXSIZE on line seven and eight I'm not sure what purpose the "tbl[n] play and tbe[0] in 9 and 10 I'm completely lost too...can someone help me out with this?

Mike
Jan 18 '07 #1
1 1228
Ganon11
3,652 Expert 2GB
Hello,

Like I've said C++ is very new to me and I have some questions...

Expand|Select|Wrap|Line Numbers
  1. 1.       #include <iostream>
  2. 2.      using namespace std;
  3. 3.       const int MAXSIZE = 10;
  4. 4.       main(){
  5. 5.            int tbl[ MAXSIZE ], n;
  6. 6.            n = 0;
  7. 7.             while( ( n < MAXSIZE ) && ( cin >> tbl[ n ] ) ) { n++; } 
  8. 8.            cout << “FIRST ELEMENT       ” << tbl[ 0 ] << endl;
  9. 9.            cout << “MIDDLE ELEMENT   ” << tbl[ n / 2 ] << endl;
  10. 10.        cout << “LAST ELEMENT        ” << tbl[ n – 1 ] << endl;
  11. 11.        return 0;
  12. 12.   }//MAIN
please correct my terminology if it's wrong.

I'm having a hard time understanding this program...I dont even know where to begin. I'm not sure why in line seven I understand the first part of the loop. But if tbl is declared with the varible MAXSIZE on line seven and eight I'm not sure what purpose the "tbl[n] play and tbe[0] in 9 and 10 I'm completely lost too...can someone help me out with this?

Mike
First, I'm going to move this thread to the C/C++ forum, where you're much more likely to get an accurate answer.

Next, I'll try and summarize what this code does as well as I can.

Lines 1 and 2 are standard and should require no explanation.

Line 3 defines MAXSIZE to be a constant variable (type int, value 10). This is used to limit the size of your array to 10 elements.

Line 5 declares an int n and Line 6 sets it to 0. n is the index for your array - it is first used to initialize each value in you array, and later, to access certain elements.

Line 5 also declares your array tbl with 10 elements, although the const MAXSIZE is used. Thus, if you changed the value in MAXSIZE, there would more more/less elements.

Now is where you might start to have trouble. Line 7 is a complicated line, so I'll try to break it down.

You're entering a while loop, which will get values from the user to put into the array. The first condition is that n < MAXSIZE. This ensures that you do not try to put values into an area that is not controlled by the array. The second condition isn't exactly a condition - it will prompt the user for input into the nth value of tbl. As long as the user enters a proper int value, this will return true - thus, as long as n is less than MAXSIZE and the user enters an int value, the loop will execute. If the user hits a bad value - a character, for example - then cin will return false, and your loop will stop executing.

The body of your loop will increment n, so as long as the input was fine and you haven't filled the array, you continue initializing values to tbl.

After your loop executes, n will be the index of the last number entered into tbl. Note that this is not necessarily the same as MAXSIZE, because if the user hits a bad value for the 6th element, there will only be 5 elements in the array, and so n would be 4.

The final few lines print various values of tbl and then and your program.

Please feel free to ask any more questions you may have.
Jan 19 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Jason Curl | last post by:
Greetings, I have an array of 32 values. This makes it extremely fast to access elements in this array based on an index provided by a separate enum. This array is defined of type "unsigned long...
21
by: Steve | last post by:
Hi, I have a form containing a lot of various forms objects (about 120-150). It takes about 10 minutes for the solution to load (only one project). If I modify the corresponding .h file and...
5
by: le007 | last post by:
:) Hey All, I've recently tried my hand with CSS. I know a good bit of html and thats more or less it. I've moved back to Ireland from Australia now and I'm trying to put a site together to keep...
3
by: settyv | last post by:
Hi, I need to generate PDF stream when i click on Linkbutton in datagrid ..At present i hardcoded the DMS Id and now it is working.But i need to pass DMS ID when click linkbutton.How can i do...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.