0

-replace each element with its integer for (2.72,4.34,9.82, 1.0,4.05,2.45) cout 2 4 9 1 4 2.

i tried this

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int n;
    double v[100],i;
    cin>>n;
    for(i=1;i<=n;i++)
        cin>>v[i];
    for(i=1;i<=n;i++)
    {
        round(v[i]);
    }
    for(i=1;i<=n;i++)
        cout<<v[i]<<" ";
}

but i get this error :

|10|error: invalid types 'double [100][double]' for array subscript|
|13|error: invalid types 'double [100][double]' for array subscript|
|16|error: invalid types 'double [100][double]' for array subscript|
  • 1
    Why are you using a double as an array subscript? – 1201ProgramAlarm Feb 17 '20 at 01:12
  • Also, arrays begin at **zero** in C++. So, instead of `for(i=1;i<=n;i++)` use `for(i=0;i – Adrian Mole Feb 17 '20 at 01:15
  • 1
    This should be of use: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – eerorika Feb 17 '20 at 01:16
  • thank you everyone. im pretty dumb. and tired. thanks. i fixed it, but now..is there a big difference between for(i=1;i<=n;i++) and (i=0;i – Costinelul Feb 17 '20 at 01:21
  • *is there a big difference* -- There is a huge difference. The first includes `n` in the loop, while the second doesn't. If `n` is 100, that could mean the difference between a program working, or a program not working and you spending hours, possibly days, trying to debug what is wrong due to the off-by-one bug. – PaulMcKenzie Feb 17 '20 at 02:04

1 Answers1

0

I see the following questions and suggestions:

  1. The iterator of an array should use an integer variable, not a double.
  2. According to the example you gave, you should round down instead of rounding, so you should use floor instead of round. It should be v[i] = floor(v [i]);.
  3. Generally speaking, if the array size is not fixed, dynamic allocation will be used. For example, use new function double *v = new double[n];, and remember to delete when using up.
  4. Array v contains 100 elements. It starts from v[0], v[1]...to v[99], so variable I is better to start from 0 to prevent overstepping.