This has been eating at me all day. I'm relatively new to C, and I CANNOT get this working and I have no idea why. I have 3 files..
I also want to apologize for the spacing as this is my first time using stack overflow... but anyways, here is the code...
assignment_1.h-----------------------------------------------------------
#ifndef ASSIGNMENT_1_H
#define ASSIGNMENT_1_H
#define NULL 0
int CalculateFactorial(int input);
int CalculateFibonacci(int input);
void ReverseArray(int size, int array[]);
void ShuffleArray(int size, int array[]);
#endif
Main.c-----------------------------------------------------------------------
#include "assignment_1.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
int fact = CalculateFactorial(15);
int fib = CalculateFibonacci(15);
printf("Factorial: %d\n", fact);
printf("Fib: %d\n", fib);
int array[] = {1,2,3,4,5,6};
int size = 6;
ReverseArray(size, array);
ShuffleArray(size, array);
return 0;
}/*end main*/
assignment_1.c---------------------------------------------------------------
#include "assignment_1.h"
int CalculateFactorial(int input){
if(input<=0) return 0;
int factorial = input;
int multiplier = input;
while(multiplier > 1){
factorial *= multiplier;
--multiplier;
}/*end while*/
return factorial;
}/*end calcfact*/
int CalculateFibonacci(int input){
if(input<=0) return 0;
else if(input == 1) return 1;
return CalculateFibonnaci(input-1) + CalculateFibonacci(input-2);
}/*end calcfib*/
void ReverseArray(int size, int array[]){
int last = size-1;
int first = 0;
int temp;
while (last-first > 1){ /*stops the loop if size is even*/
temp = array[last];
array[last] = array[first];
array[first] = temp;
++first;
--last;
if(last-first == 2) break; /*stops loop if size is odd*/
}/*end while*/
int i;
for (i = 0; i< size;++i){
printf("%d, ",array[i]);
}
printf("\n");
}/*end reverse*/
void ShuffleArray(int size, int array[]){
srand((unsigned int)time(NULL));
int i;
for (i = 0; i < size; ++i){
int idx = rand()%size; /*random unsigned int between 0 and the
max index of the array*/
int temp = array[i];
array[i] = array[idx];
array[idx] = temp;
}/*end for loop*/
for (i = 0; i< size;++i){
printf("%d, ",array[i]);
}
printf("\n");
}/*end shuffle*/
I keep getting a ton of errors all saying warning: implicit declaration of function '']
I greatly appreciate any help...
Since you're compiling assignment_1.c separately to main.c (which is reasonable if you plan on using a linker to link the two together once they're compiled), you need to include these headers in that file, too ; they don't automatically carry over from one translation unit to the other.
Insert these into assignment_1.c:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
I spotted an additional error, being the definition of NULL
you specified in assignment_1.h; you should never define NULL
yourself, as it's a standard symbol. That'd be like writing your own printf
and scanf
.
NULL
is defined within the <stddef.h>
header. Include that, too, when you want to use NULL
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.