1

I have been practising with arrays for a bit and I have encountered a problem I can't seem to find the answer for. I am trying to display the numbers the user enters, however they are not turning out as I expected. It should be in a form of a column.

#include <stdio.h>
int main (void)
{
   double A[5], B[5];
   int i;
   for (i=0; i<=4; i++)
   {
    printf("Enter 5 numbers for column A: ");
    scanf("%lf", &A[i]);
   }
   for (i=0; i<=4; i++)
   {
    printf("Enter 5 numbers for column B: ");
    scanf("%lf", &B[i]);
   }
   printf("A = (%f)  B = (%f) \n", A[i], B[i]);
   return 0;
}

The printf statement seems to be correct however numbers are not showing in the output.

4
  • 3
    Some rubber duck debugging could be useful here. Hint: How many times are you reading input? How many times are you printing output? Commented Dec 4, 2018 at 17:09
  • 1
    Add a bracket after main ,you forgot it. Commented Dec 4, 2018 at 17:12
  • @Someprogrammerdude has the answer, I think Commented Dec 4, 2018 at 17:13
  • By the time you invoke printf, i == 5.And of course I assume that your aim is to print the last number of every array , right? Commented Dec 4, 2018 at 17:16

2 Answers 2

2

You should ask yourself, what is the value of i, when printing the final output. You should also ask yourself, what is in array A and B at index i.

Given these are understood, we can display the content of an array in the same fashion as it is filled.

#include <stdio.h>
int main (void)
{
  double A[5], B[5];
  int i;
  for (i=0; i<=4; i++)
    {
      printf("Enter 5 numbers for column A: ");
      scanf("%lf", &A[i]);
    }
  for (i=0; i<=4; i++)
    {
      printf("Enter 5 numbers for column B: ");
      scanf("%lf", &B[i]);
    }
  for (i=0; i<=4; i++)
    {
      printf("A = (%f)  B = (%f) \n", A[i], B[i]);
    }
  return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

0

As said by @Tsakiroglou Fotis, you forgot to add bracket after the main function and also you are not looping the final print statement to print all the elements. Try using editors that does take care of such mistakes. here is your corrected code

#include <stdio.h>
int main (void){
double A[5], B[5];
int i;
for(i=0; i<=4; i++)
{
    printf("Enter 5 numbers for column A: ");
    scanf("%lf", &A[i]);
}
for(i=0; i<=4; i++)
{
    printf("Enter 5 numbers for column B: ");
    scanf("%lf", &B[i]);
}

for(i=0; i<5; i++){
  printf("A = (%f)  B = (%f) \n", A[i], B[i]);
}
return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.