A little question about c language

  • Context: Undergrad 
  • Thread starter Thread starter nenyan
  • Start date Start date
  • Tags Tags
    C language Language
Click For Summary

Discussion Overview

The discussion revolves around a C programming question regarding the handling of an array and input/output operations. Participants explore issues related to reading input values into an array, the implications of array size, and the correct usage of the scanf function.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant notes that the loop for reading input only iterates from 0 to 6, thus only allowing for 7 inputs, despite the possibility of entering more values.
  • Another participant suggests modifying the loop condition to allow for 8 inputs, proposing alternatives such as "for (i= 0;i<= 7;i++)" or "for (i= 0;i< 8;i++)".
  • A participant points out that the newline character in the scanf format string should be removed to avoid input issues.
  • Concerns are raised about potential array overflow if the array is declared with a size of 7 while attempting to input 8 values, suggesting a change to double a[8].
  • Multiple participants emphasize the importance of adjusting the array size to prevent overflow and potential errors in the program.

Areas of Agreement / Disagreement

There is a general agreement on the need to adjust the array size and the input handling, but the exact implementation details and the implications of the current code remain contested.

Contextual Notes

Participants express uncertainty regarding the implications of using scanf with the newline character and the risks associated with array overflow. The discussion does not resolve these concerns fully.

Who May Find This Useful

Individuals learning C programming, particularly those interested in input/output operations and array management.

nenyan
Messages
67
Reaction score
0
/*find abs value of an array*/
int absv(double *a, int n)
{
int i;
for(i=0;i<n;i++)
{if (a<0)
a=-a;

}

}


int main()
{
double a[7];
int i,j;
for(i=0;i<7;i++)
scanf("%lf\n",&a);
absv(a,7);
for(j=0;j<7;j++)
printf("%e\n",a[j]);

printf("%d,%d",i,j);

}

The result is weird. I can inpute 8 numbers but it output 7 numbers.
You can see both i and j is 7.
Why I can inpute a[7], but I can not output it?
 
Physics news on Phys.org
nenyan said:
/*find abs value of an array*/
int absv(double *a, int n)
{
int i;
for(i=0;i<n;i++)
{if (a<0)
a=-a;

}

}


int main()
{
double a[7];
int i,j;
for(i=0;i<7;i++)

So i goes from 0 to 6, 7 values, not 8. You can type all the numbers you like, but this will read in only 7 numbers and then output 7 numbers. If you want to do 8 numbers use either
"for (i= 0;i<= 7;i++)" or "for (i= 0;i,< 8;i++)".

scanf("%lf\n",&a);
absv(a,7);
for(j=0;j<7;j++)
printf("%e\n",a[j]);

printf("%d,%d",i,j);

}

The result is weird. I can inpute 8 numbers but it output 7 numbers.
You can see both i and j is 7.
Why I can inpute a[7], but I can not output it?

You may have typed 8 numbers but you didn't input a[7] because i only went up to 6.
 
Thanks.
scanf("%lf\n",&a);
I should get rid of "\n".
 
In addition to Halls's advice, be sure to change the declaration of a to

double a[8];

Otherwise you will overflow the array with possibly disastrous results when you input a[7].
 
oh, yes.
awkward said:
In addition to Halls's advice, be sure to change the declaration of a to

double a[8];

Otherwise you will overflow the array with possibly disastrous results when you input a[7].
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 4 ·
Replies
4
Views
2K