Vector exercise, do I need help?

Asked

Viewed 71 times

-2

Question: Read two vectors: R of 5 elements and S of 10 elements. Generate a vector X of 15 elements having 5 first positions containing the elements of R and the last 10 positions, elements of S. Write the vector X.

I first placed the values of the vector r inside the vector x, but I could not place the values of the vector s in x, so that it was 0 to 4.

Remained the values of vector r(I got), but from 5 to 14 I could not put the s.

Trying :

 int r[]=new int[5];    
    int s[]=new int[10];
    int x[]=new int[15];
    int a1=s.length-1,b=x.length-1;

    for (int i = 0; i < r.length; i++) {
        System.out.println("Digite valores para o vetor R :");
        r[i]=dado.nextInt();    

    }for (int i = 0; i < s.length; i++) {
        System.out.println("Digite valores para o vetor S :");
        s[i]=dado.nextInt();    

    }for (int i = 0; i < r.length; i++) {
    x[i]=r[i];  
    }for (int i =x.length-1; i >=0; i++) {
    x[b]=s[a1];
    b--;
    a1--;
      } 
for (int j = 0; j < x.length; j++) {
    System.out.println("Vetor X indice "+j+" : "+x[j]);

 }  
  • But you could help me with the question ?

1 answer

0

The problem is you’re overreacting to values. To solve, you can set the i like 5, hence it would begin to define the variables from the 5th vector.

for (int i = 5; i < 16; i++) {
    System.out.println("Digite valores para o vetor S :");
    s[i]=dado.nextInt();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.