Help me this exercise plz

Write a program that generates a vector of dimension 10 and its components are assigned to the first ten square numbers.

I'm a beginner with C++

here is my code:

#include "stdafx.h"



int main(int argc, char *argv)
{
int a[10];int i;
for(i=1; i<=10;i++)
a[i]=i*i;
printf("%d",a[i]);
return 0;
}
Last edited on
Indexing starts from 0: i=0; i < 10.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>  // library for standard input output
#include <vector>    // library for vectors

using namespace std;

int main() // the main function from which the program is run
{

vector<int> v (10, 0); // creates a vector of size 10 all with 0

for(int n = 0; n < v.size(); n++) // this is a loop that goes through the vector 1 by 1
v.at(n) = ((n+1)*(n+1));  // on every iteration of the loop this adds the square of the numbers 1 to 10

for(int n = 0; n < v.size(); n++) // this does the same as the loop above but just prints out the contents of the vector
cout << v.at(n) << endl;

}


I think this is what you mean
Last edited on
Topic archived. No new replies allowed.