Programming
c visual-studio-2008
Updated Thu, 02 Jun 2022 23:18:03 GMT

This code compiles in DevC++without problems but Visual Studio 2008 spits these warnings and refuses to compile. Where is my mistake?


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 99             
void mean(int b[]);
void median(int b[]);
void mode(int b[]);
void bubbleSort(int b[]);
int findFrequentValue(int b[]);
int findFrequentIndex(int b[]);
void printHistogram(int b[]);
int * fillResponseArray(int response[]);
int main(void) {
srand(time(NULL));
int response[SIZE]={0}; //first fill with 0's
fillResponseArray(response);
mean(response);
median(response);
mode(response);
return 0;
}     
int * fillResponseArray(int response[]){
int i;
for(i=0;i<SIZE;i++)
    response[i]=rand()%10;
return response;
}
/*finds mean*/
void mean(int b[]) {
int sum=0;
int i;
printf( "********\n"
    "MEAN\n"
    "********\n\n"
    "The mean is the average value of the data\n"
    "items. The mean is equal to the total of\n"
    "all the data items divided by the number\n"
    "of data items.\n" );
for(i=0;i<SIZE;i++)
    sum+=b[i];
printf ( "\n%d given responses SUM: %d MEAN: %.2f\n", SIZE, sum, (double)sum/SIZE         );
printf("\n");
}
/*finds median*/
void median(int b[]) {
int i;
printf( "********\n"
"MEDIAN\n"
"********\n");
printf("\nUnsorted array\n");
for(i=0;i<SIZE;i++){
    if(i!=0&&i%10==0)
        printf("\n");
    printf("%d ",b[i]);
}
printf("\n");
bubbleSort(b);
printf("Sorted array\n");
for(i=0;i<SIZE;i++){
    if(i!=0&&i%10==0)
        printf("\n");
    printf("%d ",b[i]);
}
printf("\n\nMEDIAN is the %d th element: %d",SIZE/2,b[SIZE/2]);
printf("\n");
}
/*finds mode*/
void mode(int b[]) {
int frequency[10]={0},i;
printf( "\n********\n"
    "MODE\n"
    "********\n");
printf("Mode is the most frequent value.\n");
/* Fill the frequency array with occurrences of indexes*/
for(i=0;i<SIZE;i++)
    frequency[ b[i] ]++;
/*print frequency array*/
for(i=0;i<10;i++)
    printf("frequency[%d]= %2d\n",i,frequency[ i ]);
/* print result */
printHistogram(frequency);
printf("\n");
printf("\nMode is %d and appeared %d times", findFrequentIndex(frequency),        findFrequentValue(frequency) );
}
/* Returns the index of most frequent one in frequency array*/
int findFrequentIndex(int b[]){         
int mostFrequent=b[0],i;
int rating=0;
for(i=1;i<10;i++){
    if(b[i]>mostFrequent){
        mostFrequent=b[i];         
        rating=i;               //keep index of mostFrequent one!
    }
}
return rating;
}
/* Returns the value of most frequent occurrence in frequency array*/
int findFrequentValue(int b[]){         
int mostFrequent=b[0],i;
for(i=0;i<10;i++){
    if(b[i+1]>mostFrequent)    //Achtung !!!!!
        mostFrequent=b[i+1];
}
return mostFrequent;
}
/*prints the histogram*/
void printHistogram(int b[]){
int i,j;
for(i=0;i<10;i++){
    printf("\nfrequency[%d] ",i);
    for(j=1;j<=b[i];j++){
            printf("*");
    }
}
}
/*Sort function*/
void bubbleSort(int b[]) {    //Passed by reference
int passes=SIZE-1;
int i,j,temp;
for(i=1;i<=passes;i++){
    for(j=0;j<SIZE-1;j++){
        if(b[j]>b[j+1]){
            temp=b[j+1];
            b[j+1]=b[j];
            b[j]=temp;
        }
    }
}
}

1>c:\users\turan\documents\visual studio
2008\projects\project1\search\search\dsa.c(30) : warning C4024:
'fillResponseArray' : different types for formal and actual parameter
1 1>c:\users\turan\documents\visual studio
2008\projects\project1\search\search\dsa.c(32) : error C2065:
'response' : undeclared identifier 1>c:\users\turan\documents\visual
studio 2008\projects\project1\search\search\dsa.c(32) : warning C4047:
'function' : 'int *' differs in levels of indirection from 'int'
1>c:\users\turan\documents\visual studio
2008\projects\project1\search\search\dsa.c(32) : warning C4024: 'mean'
: different types for formal and actual parameter 1
1>c:\users\turan\documents\visual studio
2008\projects\project1\search\search\dsa.c(33) : error C2065:
'response' : undeclared identifier 1>c:\users\turan\documents\visual
studio 2008\projects\project1\search\search\dsa.c(33) : warning C4047:
'function' : 'int *' differs in levels of indirection from 'int'
1>c:\users\turan\documents\visual studio
2008\projects\project1\search\search\dsa.c(33) : warning C4024:
'median' : different types for formal and actual parameter 1
1>c:\users\turan\documents\visual studio
2008\projects\project1\search\search\dsa.c(34) : error C2065:
'response' : undeclared identifier 1>c:\users\turan\documents\visual
studio 2008\projects\project1\search\search\dsa.c(34) : warning C4047:
'function' : 'int *' differs in levels of indirection from 'int'
1>c:\users\turan\documents\visual studio
2008\projects\project1\search\search\dsa.c(34) : warning C4024: 'mode'
: different types for formal and actual parameter 1 1>Build log was
saved at "file://c:\Users\Turan\Documents\Visual Studio
2008\Projects\Project1\Search\Search\Debug\BuildLog.htm" 1>Search - 5
error(s), 9 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========




Solution

You can't declare a variable after any non-declarations:

int main(void)
{
    srand(time(NULL));
    int response[SIZE]={0};  // This can't go here!
    ...

in Visual Studio. You need to declare your variables at the top of the scope:

int main(void)
{
    int response[SIZE]={0};
    srand(time(NULL));
    ....

It's a feature of modern C that Visual Studio's C compiler doesn't have.

(There may be other problems with your code, but you're going to have to fix that one first!)





Comments (3)

  • +0 – Thanks for this information. Code worked after your suggestion. How can I upgrade Visual Studio's compiler? — Apr 30, 2013 at 15:51  
  • +0 – @user1939432 - you can't. You have to wait for Microsoft to become C99 compliant. — Apr 30, 2013 at 15:54  
  • +1 – Yeah, be patient. C99 is only 14 years old. — Apr 30, 2013 at 17:55