help with a question please.

im trying to write a program that populates an array of 100 interger elements with the numbers from 1 to 100 and then outputs the numbers in the array......im very new at this stuff and not very good at this, but i would appreciate the help...this is all i have as of now.

#include<iostream>
#include<iomanip>
using namespace std;

main()
{
int arr[100];
int i,ctr;


for(ctr=1;ctr<=100;++ctr)

cout<<setw(8)<<arr[i];


cout << "\n\n";

system("pause");
return 0;
}[/output]
Here's what you need to do:

1.) Assign a number to each element of the array
2.) Print all the elements of the array

There are several things that are wrong with your code:

1.) In the for loop, you're giving a value to ctr, and then comparing and incrementing it. However, you use i to access the indices of the array. This is incorrect, because:

a.) i was never initialized
b.) i never changes

2.) The for loop is incorrect to begin with, because you're attempting to print the values from an array that hasn't been initialized.

What you should do, is make one for loop which initializes the array, and then another for loop which prints each elements of the array.

1
2
3
4
5
6
const unsigned int size = 100;
int array[size];

for(int i=0; i<size; ++i) {
	array[i] = i+1;
}


Let's see if you can figure out the rest.
ok, my visual studio keeps crashing so i have no way to compile this until i can get it fixed. so i'am attempting to figure this out on paper. this is what i got so far.

#include<iostream>
#include<iomanip>

using namespaace std;

int main ()
{

const int size=100;
int arr[size];
int i;

for(i=0;i<size;++i)
{
arr[i]=i+1;
}

for(i=0;i<size;++i)
{
cout<<setw(8)<<arr[i]<<"\n";
}

return 0;
}

like i said i cant compile this until i can fix visual studios, so would this work for my initial question? if not what do i need to do? thanks.
Last edited on
ok, my visual studio keeps crashing so i have no way to compile this until i can get it fixed.

Fix this first. Run updates and if that still does not work try a re-install\repair of the product. IDE's usually crash because they are configured incorrectly, so you may have to uninstall it, run something and like CCleaner before you re-install the product again; if it gets to this point try using the default installation settings.

What version of MS VS are your running? What OS are you running it on?

For now you can use an online gadget like this one to check your code: http://coliru.stacked-crooked.com/

You have a typo on Line 4.
idk, its very strange now when i try to compile my avast anti-virus blocks it ive tried to re installing it but it still does it. visual express 2010 on windows 7. also i ran it on that link you gave me and it does output the array 1-100 but it does it all in one column i dont know if thats the way the link shows it or if im missing something in my code to output it 10 by 10 columns and rows. and thanks for pointing out the typo in line 4.
Last edited on
That's not a crash, that's the anti-virus doing it's job. This is a very common issue and you just need to add the directory you compile into as an exception to what ever active scan component Avast! uses. Here you go, from the third entry down on Google: http://forum.avast.com/index.php?topic=90553.0
Topic archived. No new replies allowed.