Somethings Wrong

any body help me for one dimentional using for loop statement..? like this

#include<conio.h>
#include<iostreamh.h>
main()
{
clrscr();
int i;
int x[11];
for (i=0;i<10;i++)
{
x[i]=0+i;
}
for (i=0;i<10;i++)
{
cout<<x[i];
}
for (i=0;i<11;i++)
{
x[i]=0+i;
}
for (i=0;i<11;i++)
{
cout<<x[i],cout<<"\n";
}
getch();
return 0;
}

The Format like this...!

012345678910
1
2
3
4
5
6
7
8
9
10
What kind of output were you expecting? Also, your syntax is wrong:

 
cout << x[i],cout << "\n";


...should be:

 
cout << x[i] << "\n";


...or:

 
cout << x[i] << endl;


Also, you're using deprecated header files. Where are you learning C++?
the <<"\n"; end <<endl; it also desame right?

why can i change the for (i=0;10;i++)

the output is

01234567890 not 10...?

what is the code for that the full code...

you know that..?
Last edited on
can you run my code end solve it ....?

paupau..
First of all, you dim'ed x as 11, ( 0 - 10 ), but only assigned 0 to 9 in the loop.

for (i=0;i<10;i++)

Second, the ending zero on the line was the first of the x[i] array from the next for loop. So, I added a newline before starting the second for loop. Also, there was no need to re-initialize the x[i] array, so I removed it.

Here is your program listing that runs under VC++ express 2008.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// one dim array.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include "conio.h"
#include <Windows.h>
using namespace std;

void ClrScr()
{
	COORD c;
	DWORD NumWr;
	HANDLE hnd = GetStdHandle(STD_OUTPUT_HANDLE) ;
	c.X=0;
	c.Y=0;
   
    FillConsoleOutputCharacter(hnd,' ',80*25,c,&NumWr);
}

int main()
{
ClrScr();
int i;
int x[11];

for (i=0;i<11;i++)
  x[i] = i;

for (i=0;i<11;i++)
   cout << x[i]<< " ";

cout << "\n";
 
for (i=0;i<11;i++)
   cout << x[i] << "\n";

_getch();
return 0;
}


L8R..
Last edited on
@whitenite1

>DIM'ed

What is this? Visual Basic?
@packetpirate

I'm use to other programming languages. So, to me, when when a variable gets bracketed by [], or (), it has more than one dimension. Sorry for the Freudian slip. I guess I should have just said, 'You created an array of 11 elements, but only assigned 10 of them, 0 through 9'. Thanks for bringing it to my attention. Writing things like that could just throw a new programmer.
Topic archived. No new replies allowed.