This program is meant to read in strings, calculate the average word length then printout all the strings that are greater than the average. My issue is with the function in red, i'm not sure how to call it or make it work, it doesnt like the strlen parameter nor the printf parameters. It is meant to printout all strings greater than the average length.

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

#define	STRING_LEN 80
#define ARRAY_LEN 5

void *emalloc(size_t s){
   void *result = malloc(s);
   if (NULL == result){
      fprintf(stderr, "Memory allocation failed!\n");
      exit(EXIT_FAILURE);
  }
   return result;
}

void print_out(char *a, double avg, int nums, int n){
   if(n < nums){
       if(avg < strlen(a[n])){
         printf("%s", a[n]);
     }print_out(a, avg, nums, n+1);
    }
}

int main(void){
   char word[STRING_LEN];
   char *wordlist[ARRAY_LEN];
   int num_words;
   double average;
   int n = 0;
   int i;
  num_words = 0;

   while(num_words < ARRAY_LEN && 1 == scanf("%79s", word)){
   wordlist[num_words] = emalloc((strlen(word) + 1) * sizeof   wordlist[0][0]);
      strcpy(wordlist[num_words], word);
     num_words++;
   }

   average = 0.0;
   for (i=0; i<num_words; i++){
      average += strlen(wordlist[i]);
   }

   average = average / num_words;
   fprintf(stderr, "%f\n", average);

  print_out(wordlist, average, num_words, n);
   
   for (i=0; i<num_words; i++){
      free(wordlist[i]);
   }

   return EXIT_SUCCESS;
}

Recommended Answers

All 3 Replies

Use code tags. [code=C]<Your code here> [/code].
Can you give us the output of the compiler? That would make answering your question alot easer.

Ask your question in the C forum.

Ask your question in the C forum.

What? Last time I checked this is the C forum.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.