A little question about c language

In summary, The conversation discusses finding the absolute value of an array and includes a code example. It is mentioned that there is an error in the code where only 7 numbers are input and output instead of 8. The issue is attributed to the loop only going up to 6 instead of 7 or 8. The solution is to change the declaration of the array to allow for 8 elements.
  • #1
nenyan
67
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
  • #2
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.
 
  • #3
Thanks.
scanf("%lf\n",&a);
I should get rid of "\n".
 
  • #4
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].
 
  • #5
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].
 

1. What is the C programming language?

The C programming language is a general-purpose, high-level programming language that was developed in the early 1970s by Dennis Ritchie. It is widely used in the development of operating systems, system software, application software, and games.

2. What are the main features of C language?

The main features of C language include portability, modularity, efficiency, low-level access to memory, rich library support, and the ability to manipulate bits and bytes directly.

3. What are the advantages of using C language?

C language is a popular choice for programming due to its efficient and simple syntax, powerful library support, and compatibility with different platforms. It also allows for low-level memory manipulation, making it suitable for system-level programming.

4. Is C language difficult to learn?

While C language may have a steep learning curve for beginners, it is not necessarily difficult to learn. With practice and dedication, one can become proficient in C language and harness its power for various programming tasks.

5. What are some common applications of C language?

C language is used in a wide range of applications, including system programming, developing operating systems, embedded systems, game development, and scientific and mathematical computing. It is also commonly used in the development of databases, compilers, and interpreters.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
871
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
917
Replies
3
Views
2K
Back
Top