0

Hi, I am new to C and C++. This is my first course on the subject and my instructor handed out a printout in class with code on it to get us started, but he did not go over any other code or teach us any C. I can only assume he expected us to go online and figure it out for ourselves, but I am having problems. This is the code that he gave us to get us started but I can't get it to compile. Please help.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef struct Node {
  int first;
  struct Node = rest;
  } Node;

typedef Node * List;

main() {

void greeting() {
  printf("\nThis Program will sort and seperate a list into primes.\n");
  printf("You may repeat the program as often as you want.\n\n");
  }

void goodbye() {
  printf("\nThank you for experimanting.\n");
  printf("Goodbye.\n\n");
  }

int respondYesTo(char * question) {
  char answer;
  printf("%s? y/n: ", question);
  scanf("\n%c", &answer);
  return answer == 'y' || answer == 'Y';
}

void doExperiments() {
  int count = 0;
  while(respondYesTo("Do an experiment")) {
	count++;
	oneExperiment();
	}
  printf("You have preformed %d experiments.\n", count);
}

void oneExperiment(){
  if (respondYesTo("Generate some data")) generateDataExperiment();
  else if (respondYesTo("Append data")) appendDataExperiment();
  else if (respondYesTo("Reverse data")) reverseDataExperiment();
  }

void generateDataExperiment() {
  int datasize, a, b;
  List data, reverseData;
  if (respondYesTo("Make a random list")) {
	getInteger(&dataSize, "Enter data size");
	getInteger(&a, "Enter lower limit");
	getInteger(&b, "Enter upper limit");
	data = makeRandomList(dataSize, a, b);
	}
  else if (respondYesTo("Make a sequential list")) {
		getInteger(&a, "Enter start of sequence");
		getInteger(&b, "Enter end of sequence");
		data = integers(a,b);
		}
  displayList(data, "Your list:");
  reversedData(reversedData, "Reversed data:");
  displayList(data, "Original data after reversing:");
}

void appendDataExperiment() {
  printf("The append data experiment is not implemented.\n");
}

void reverseDataExperiment() {
  printf("The reverse data experiment is not implemented.\n");
}

List emptyList() {
  return (List) 0;
  }

List makeList(int x, List L) {
  List result = (List) malloc(sizeof(Node));
  result->first = x;
  result->rest = L;
  return result;
  }

int isEmpty (List L) {
  return L == NULL;
  }

int getFirst (List L) ( return L->first; )
List getRest (List L) ( return L->rest; )

List makeRandomList(int size, int lo, int hi) {
  List result = emptyList();
  int k;
  for (k = 0; k < size; k++)
	result = makeList(randomFrom(lo, hi), result);
  return result;
  }

List integers (int a, int b) {
  if (a == b)
	return makeList(a, listEmpty());
  else if (a < b)
	return makeList(a, integers(a+1, b));
  else
	return makeList(a, integers(a-1, b));
  }

List sortList(List L) {
  return L;
  }

List appendList (List L, List M) {
  if (isEmpty(L)) 
	return M;
  else if (isEmpty(M)) 
	return L;
  else
	return makeList (getFirst(L), appendLists (getRest(L), M));
  }

List reverseList (List L) {
  if (isEmpty(L))
	return emptyList();
  else
	return appendLists (reverseList(getRest(L)), makeList(getFirst(L), EmptyList();
  }

void displayList(List L, char * msg) {
  int numberPrinted = 0, COLS = 10, WIDTH = 6;
  List p = L;
  printf("%s\n", msg);
  if (isEmpty(L)) {
	printf(" E M P T Y\n"); return;
	}
  while (! isEmpty(p)) {
	printf("%*d", WIDTH, getFirst(p));
	numberPrinted++;
	if (numberPrinted%COLS == 0)
		printf("\n");
	p = getRest(p);
	}
  if (numberPrinted%COLS != 0)
	printf("\n");
  }

And here is the list of compile errors

/pa3.c:16: error: expected identifier or ‘(’ before ‘=’ token
/pa3.c:17: warning: no semicolon at end of struct or union
/pa3.c: In function ‘generateDataExperiment’:
/pa3.c:59: error: ‘dataSize’ undeclared (first use in this function)
/pa3.c:59: error: (Each undeclared identifier is reported only once
/pa3.c:59: error: for each function it appears in.)
/pa3.c:67: warning: assignment makes pointer from integer without a cast
/pa3.c: In function ‘makeList’:
/pa3.c:89: error: ‘Node’ has no member named ‘rest’
/pa3.c: In function ‘main’:
/pa3.c:97: error: expected declaration specifiers or ‘...’ before ‘return’
/pa3.c:98: error: ‘getFirst’ declared as function returning a function
/pa3.c: In function ‘getFirst’:
/pa3.c:98: error: expected declaration specifiers or ‘...’ before ‘return’
/pa3.c:100: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘List’
/pa3.c:108: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
/pa3.c:117: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
/pa3.c:121: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
/pa3.c:130: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
/pa3.c:137: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
/pa3.c:155: error: expected declaration specifiers before ‘}’ token
/pa3.c:155: error: expected ‘{’ at end of input
/pa3.c: In function ‘main’:
/pa3.c:155: error: expected declaration or statement at end of input
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by drkybelk
0

Hi,
you have to take your function definitions outside the main body. It looks a bit like java what you've done here. Inside the main loop you should *CALL* the functions you defined outside.

void greeting() {
printf("\nThis Program will sort and seperate a list into primes.\n");
printf("You may repeat the program as often as you want.\n\n");
}

int main(int argc, char** argv) {
   greeting();
   return 0;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.