0

the last array array 3 is not giving me the desired output. i see no logic errors therefore i do not know why i am getting the wrong output.

#include<stdio.h>

    int arr1[5]={55,145,950,25,500};
    int arr2[5]={44,58,98,25,62};
    int arr3[5],array1,array2,array3;
    int i=0,j=0,k=0;

    for(i=0;i<5;i++){
        array1=arr1[i]*2;
        printf("array 1 doubled is %d . \n",array1);
        //i++;

}
for(j=0;j<5;j++){
    array2=arr2[j]+3;
        printf("Array 2 increased by 3 is %d \n",array2);
        //j++;
    }
    for(k=0; k<5; k++){
        arr3[k]=array1+array2;
        //printf("the reverse order after operations is %d",arr3[0]);
        //k++;
        printf("reverse order is %d\n",arr3[k]);

 }

}
3
Contributors
2
Replies
14
Views
1 Year
Discussion Span
Last Post by Nutster
0

array1 and array 2 are declared as int. This means that you are overwriting the values in them and the last loop is re-using the last value stored in them. Without a clear idea of the desired outcome it's hard to arrive at a solution.

Edited by tinstaafl

0

In your third loop, you are assigning each element of arr3 with the sum of the last values of array1 and array2, which would be 1065. To get the array reversed, you need to reference an array subtracting the index from the end. e.g.

printf("The reversed array is: ");
for (i=0; i<5; ++i)
{
    array3 = arr1[5-i];
    printf("%i ", array3);
}
printf("\n");
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.