Thread: character array

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    19

    character array

    Hello all! I am hoping someone can help me with this because my professor wont get back to me(he lacks programming skills, I believe). Anyways, I am supposed to create an array with a user specified amount(between 10 and 30) of random letters to be generated. After the amount of characters has been specifed, the program should send the base address of that memory and its size to a random function(which I thought I created correctly) and fill the memory with random capital letters. After this function has completed, it will go back to the main program and prompt the user to enter a capital letter to be searched for(using the search function, which I also thought I did correctly). In the search function, it should print out the index locations of each and every match between the random letters and the letter being searched for. Hope it makes sense. I have provided the code that I have so far. Another thing, it is in 4 files: array.h, array.c, random.c, and search.c...I thank you for helping in advance!

    Code:
    /*array.h*/
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    int random(char *data, int num);
    int search(char *data, char ch, int num);
    
    /*array.c*/
    #include "array.h"
    
    int main(void){
    
      int num;
      char ch;
      char *data;
    
      printf("%s%s",
            "Enter the number of random letters to be generated \n",
            "(MUST BE BETWEEN 10 AND 30): ");
      scanf("%d", &num);
      fflush (stdin);
      while(num < 10 || num > 30){
        printf("Error: Must be between 10 and 30! Try Again:\n");
        scanf("%d", &num);
        fflush (stdin);
      }
    
      data = calloc(num, sizeof(char));
    
      int random(char *data, int num);
    
      printf("Enter a CAPITAL LETTER to be searched for:\n");
      scanf("%c", &ch);
      while(!isupper(ch)){
        printf("Error: Must be a capital letter, Try Again:\n");
        scanf("%c", &ch);
      }
    
      int search(char *data, char ch, int num);
    
      return 0;
    }
    
    /*random.c*/
    #include "array.h"
    
    int random(char *data, int num){
    
      srand(time(NULL));
      data[num] = (rand() % 26) + 65;
    
      return 0;
    }
    
    /*search.c*/
    #include "array.h"
    
    int search(char *data, char ch, int num){
    
      int n;
    
         for(n = -1; n < num; ++n)
            if(data[n] = ch)
              printf("data[%d] = %c\n", data[n], ch);
    
       return 0;
    }

  2. #2
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    all your information you gave us is good and will be helpful, but you forgot one important thing-what in the program ins't working right???? We'll help you with your homework, but if you just give us the code and ask us to fix it, that's the same as us writing it all for you, which we don't do.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    19
    The code reads in the number of letters as well as the capital letter itself, however, it doesnt create the random array or search and print. Sorry about that...Im just looking for help, not someone to do it for me, otherwise I wouldve posted it a long time ago and not have written anything!

  4. #4
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    just a couple small things... in your first printf() statement, why do you have %s%s? you don't need those in there to print the two strings. Also, is your assignment forcing you to use pointers? You could get the base address just as well by using the & operator, and it would probably make your code easier to write.
    [edit]
    instead of your while(!isupper) section you can just do toupper(ch) to make sure the character is always capital
    Last edited by Draco; 11-04-2003 at 11:52 PM.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    19
    I just used the %s%s in the first printf() statement so it would look more clear and the assignment doesnt ask specifically for pointers!

  6. #6
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    I think the problem with your two functions is that you're trying to give the pointers int values.

    [code]
    int random(char *data, int num){

    srand(time(NULL));
    data[num] = (rand() % 26) + 65;

    return 0;
    }

    Try changing data into an int instead of a pointer, and just use &data when you need the address. That should work.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You've got all kinds of problems.

    1) You cannot flush input streams. 'fflush(stdin)' is wrong. Search the board if you need more.

    2) You aren't actually calling these functions, you're prototyping them:
    Code:
     }
    
      data = calloc(num, sizeof(char));
    
      int random(char *data, int num);
    
      printf("Enter a CAPITAL LETTER to be searched for:\n");
    And...
    Code:
    }
    
      int search(char *data, char ch, int num);
    
      return 0;
    }
    3) And if you were actually calling these, then this:
    Code:
      data[num] = (rand() % 26) + 65;
    ...would be off the end of your allocated space, and would be incorrect.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    19
    Actually, the fflush() was my teacher's idea, but thank you for letting me know, that just makes me doubt him that much more!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character Array comparison
    By magda3227 in forum C Programming
    Replies: 7
    Last Post: 07-09-2008, 08:36 AM
  2. Replies: 7
    Last Post: 05-11-2008, 10:57 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. two dimensional character array
    By feuerraeder in forum C Programming
    Replies: 4
    Last Post: 11-22-2002, 08:59 AM
  5. Array of Character Arrays
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-09-2002, 06:07 PM