'Implicit declaration of function loop1 and loop2
The errors I get on loop1 and loop2 are:
data definition has no type or storage class
and
Implicit declaration of function
loop1andloop2
Also can anyone show me a while loop version of this code.
#include <stdio.h>
int a[5] = { 5, 10, 15, 20, 25 };
int x, temp, size = 5;
int main()
{
printf("BEFORE: \n\n");
for (x = 0; x = 4; x++)
{
printf("Element [%d] is %d\n"), x, a[x];
}
loop1();
printf("n==============\nAFTER:\n\n");
loop2();
}
loop1();
{
for (x = 0; x <= 2; x++)
{
temp = a[x];
a[x];
a[(size - 1) - x] = temp;
}
}
loop2();
{
for (x = 0; x <= 4; x++)
{
printf("Element [%d] is %d\n"), x, a[x];
}
}
Solution 1:[1]
I did not try to debug your code. This is the correct definition of the functions syntactically. Based on what you need them to do, you should pass also parameters in. But, the program does not run logically correctly, you should find the problem and tell us the expected output in order to help you.
#include <stdio.h>
int a[5] = { 5, 10, 15, 20, 25 };
int x, temp, size = 5;
void loop1()
{
for (int x = 0; x <= 2; x++)
{
temp = a[x];
a[x];
a[(size - 1) - x] = temp;
}
}
void loop2()
{
for (int x = 0; x <= 4; x++)
{
printf("Element [%d] is %d\n"), x, a[x];
}
}
int main()
{
printf("BEFORE: \n\n");
for (x = 0; x = 4; x++)
{
printf("Element [%d] is %d\n"), x, a[x];
}
loop1();
printf("n==============\nAFTER:\n\n");
loop2();
}
Solution 2:[2]
There are many bugs in your program. To mention a few: Functions called before being declared. Arguments placed outside functions calls. Statement that doesn't do anything. Assignment instead of condition. Etc...
Try like:
#include <stdio.h>
int a[5] = { 5, 10, 15, 20, 25 };
int x, temp, size = 5;
void loop1()
{
for (x = 0; x <= 2; x++)
{
temp = a[x];
//a[x]; ???? why...
a[(size - 1) - x] = temp;
}
}
void loop2()
{
for (x = 0; x <= 4; x++)
{
printf("Element [%d] is %d\n", x, a[x]);
}
}
int main(void)
{
printf("BEFORE: \n\n");
for (x = 0; x != 4; x++)
{
printf("Element [%d] is %d\n", x, a[x]);
}
loop1();
printf("n==============\nAFTER:\n\n");
loop2();
}
BTW:
You need to turn up your compilers warning level.
For instance: gcc -Wall -Wextra -Werror ...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | wajaap |
| Solution 2 | Support Ukraine |
