Array

I have to write a program that declares an array of fifty elements of integer type, in which each element is equal to sum of two previous elements. When i compile the program all I get is a bunch 1s that run across the screen. I know it seems elementary but I'm just started learning programming. Any will help will be appreciated. This is what I have so far.

#include "stdafx.h"
#include <iostream>

using namespace std;

int main ()
{
for (int n=1; n>0; ++n)
{
while (n<50)
{
cout << n << ", ";
}
}
return 0;
}

This is why you are getting that as output:
1
2
3
4
5
6
7
for(int n=1; n > 0; ++n) //loops until n overflows
{
  while(n < 50) //n is initialized as 1 so this loops forever
  {
    cout << n << ",";  //outputs n followed by ,
  }
}


This is what you need to know:

1
2
//declaring an array of 50 elements
int n[50];


1
2
//setting the value of an element
n[4] = 24;


1
2
3
4
5
//looping through from a starting element to a finishing element
for(int i=0; i < 50; ++i)
{
  //code to be executed goes here...
}


1
2
//setting element value to that of another element
n[16] = n[32];


1
2
for(int i=0; i < 50; ++i)
  cout << n[a] << endl; //outputting all the contents of an array of 50 elements 



With that information you should be set. Feel free to ask if you have any other problems. And please use code tags next time.
Topic archived. No new replies allowed.