在我的程序中编译C中的表达语法错误
在运行命令中的代码时,我遇到了这个问题,但是当我使用在线编译器时,它可以正常工作。
我在命令中编译时遇到的所有错误
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARRAY_SIZE 20
#define UPPER_LIMIT 20
#define LOWER_LIMIT 12
#define DATA_UPPER_LIMIT 90
#define DATA_LOWER_LIMIT 10
// randomize and print out number
int randomAndPrint(int array[],int size)
{
int i;
array[size];
// randomize
for(int i=0;i<size;i++){
array[i]=(rand()%(DATA_UPPER_LIMIT-DATA_LOWER_LIMIT+1))+DATA_LOWER_LIMIT;
}
printf("Bu - Project 3 \n");
// print size of array
printf("Sample Size = %d \n", size);
printf("Array elements:\n");
// print out element in array
for(int i=0;i<size;i++){
printf("%d ",array[i]);
}
}
// find max and min
int maxAndMin(int array[],int size){
int min,max,i;
min=max=array[0];
// ietrate and find max and min
for(i=1; i<size; i++)
{
if(min>array[i])
min=array[i];
if(max<array[i])
max=array[i];
}
printf("\nMinimum = %d, ",min);
printf("Maximum = %d",max);
}
// find average
int average(int array[],int size){
int i,avr;
int sum=0;
// iterate and then sum to find avr
for(i=0;i< size;i++)
{
sum = sum + array[i];
}
avr = sum/size;
printf("\nAverage = %d \n",avr);
}
// sorting the array
int sort(int array[],int size){
int x, y, temp,i;
// check if the element next to each other is bigger then swap
for(x = 0; x < size - 1; x++){
for(y = 0; y < size - x - 1; y++){
if(array[y] > array[y + 1]){
temp = array[y];
array[y] = array[y + 1];
array[y + 1] = temp;
for(int i=0;i<size;i++){
printf("%d ",array[i]);
}
putchar('\n');
}
}
}
}
// finding the median
int findMedian(int array[],int size){
int avr,middle;
middle = size/2;
// check if size is even or odd
if(size%2==0){
avr = (array[middle-1]+array[middle])/2;
}
else{
avr = array[middle];
}
printf("Median = %d",avr);
}
int main(void)
{
int sizeOfArray,n;
srand(time(NULL));
sizeOfArray = (rand() % (UPPER_LIMIT - LOWER_LIMIT+ 1)) + LOWER_LIMIT;
// calling each function in the main
int array[sizeOfArray];
randomAndPrint(array,sizeOfArray);
maxAndMin(array,sizeOfArray);
average(array,sizeOfArray);
sort(array,sizeOfArray);
findMedian(array,sizeOfArray);
return 0;
}
I got this problem while I'm running the code in command but it worked fine when I used the online compiler.
All kinds of error I got when I compile in command
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARRAY_SIZE 20
#define UPPER_LIMIT 20
#define LOWER_LIMIT 12
#define DATA_UPPER_LIMIT 90
#define DATA_LOWER_LIMIT 10
// randomize and print out number
int randomAndPrint(int array[],int size)
{
int i;
array[size];
// randomize
for(int i=0;i<size;i++){
array[i]=(rand()%(DATA_UPPER_LIMIT-DATA_LOWER_LIMIT+1))+DATA_LOWER_LIMIT;
}
printf("Bu - Project 3 \n");
// print size of array
printf("Sample Size = %d \n", size);
printf("Array elements:\n");
// print out element in array
for(int i=0;i<size;i++){
printf("%d ",array[i]);
}
}
// find max and min
int maxAndMin(int array[],int size){
int min,max,i;
min=max=array[0];
// ietrate and find max and min
for(i=1; i<size; i++)
{
if(min>array[i])
min=array[i];
if(max<array[i])
max=array[i];
}
printf("\nMinimum = %d, ",min);
printf("Maximum = %d",max);
}
// find average
int average(int array[],int size){
int i,avr;
int sum=0;
// iterate and then sum to find avr
for(i=0;i< size;i++)
{
sum = sum + array[i];
}
avr = sum/size;
printf("\nAverage = %d \n",avr);
}
// sorting the array
int sort(int array[],int size){
int x, y, temp,i;
// check if the element next to each other is bigger then swap
for(x = 0; x < size - 1; x++){
for(y = 0; y < size - x - 1; y++){
if(array[y] > array[y + 1]){
temp = array[y];
array[y] = array[y + 1];
array[y + 1] = temp;
for(int i=0;i<size;i++){
printf("%d ",array[i]);
}
putchar('\n');
}
}
}
}
// finding the median
int findMedian(int array[],int size){
int avr,middle;
middle = size/2;
// check if size is even or odd
if(size%2==0){
avr = (array[middle-1]+array[middle])/2;
}
else{
avr = array[middle];
}
printf("Median = %d",avr);
}
int main(void)
{
int sizeOfArray,n;
srand(time(NULL));
sizeOfArray = (rand() % (UPPER_LIMIT - LOWER_LIMIT+ 1)) + LOWER_LIMIT;
// calling each function in the main
int array[sizeOfArray];
randomAndPrint(array,sizeOfArray);
maxAndMin(array,sizeOfArray);
average(array,sizeOfArray);
sort(array,sizeOfArray);
findMedian(array,sizeOfArray);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它在在线编译器上运行而不是在 IDE 上运行的原因是两者都使用不同的标志来编译代码。
假设您的所有代码都在一个文件中,请使用以下内容:
TIP
:如果您的函数没有返回类型,请确保使用void
。如果您将int
放在函数之前,那么编译器将假定该函数将返回整数值,但事实并非如此。另外,请确保不要声明未使用的变量,因为这是非常糟糕的做法。不管怎样,欢迎来到SO:)The reason why it works on an online compiler and not on your IDE is because both use different flags to compile the code.
Assuming all your code is in one file, use this:
TIP
: make sure to usevoid
if your function has no return type. if you putint
before a function then your compiler will assume that the function is going to return an integer value, which it doesn't. Also, make sure to not declare variables that are not used, as this is really bad practice. Anyway, welcome to SO :)