Thread: C Code Pointerss Homework

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    7

    C Code Pointerss Homework

    Hello,I have an assignment about pointers and need help to do it.I didn't understand the question. Here is the Question:

    Just modify the code in Part C in the following manner:
    1. int array[8] should be replaced by the char array[] = “EENG112 Introduction to
    Programming”
    2. modify call_by_reference array so that its input is char* and it modifies its input as
    follows:
    a. Each vowel is replaced by a character “1”,
    b. Each consonant is replaced by a character “0”.
    3. print_1D_array should be modified to print character sequence.
    Keep the original form of code in Part C, just make required modifications.

    and this is the Code in Part C:
    Code:
    
    
    Code:
    #include <stdio.h>
    void pass_by_reference(int *,int );
    void print_1D_array(int*,int );
    void print_1D_array_adress(int* ,int );
    void main(void)
    {
    int array[8] = {1,2,3,4,5,6,7,8};
    int* array_p;
    array_p = array;
    printf("Before modification\n");
    print_1D_array(array_p,8);
    //lets modify the array
    pass_by_reference(array_p,8);
    printf("After modification\n");
    print_1D_array(array_p,8);
    //lets print the adresses of the array elements
    printf("Addresses of elements of array \n");
    print_1D_array_adress(array,8);
    }
    void pass_by_reference(int *pointer,int N)
    {
    for (int i = 0;i<N;i++)
    {
    *(pointer+i) += 3;
    }
    }
    void print_1D_array(int* array,int N)
    {
    for (int i = 0;i<N;i++)
    {
    printf("%d ",*(array+i));
    }
    printf("\n");
    }
    void print_1D_array_adress(int* array,int N)
    {
    for (int i = 0;i<N;i++)
    {
    printf("%p\n",array+i);
    }
    printf("\n");
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you need to indent your code.
    Code:
    #include <stdio.h>
    void pass_by_reference(int *, int);
    void print_1D_array(int *, int);
    void print_1D_array_adress(int *, int);
    int main(void)  //!! yes, really!
    {
      int array[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
      int *array_p;
      array_p = array;
      printf("Before modification\n");
      print_1D_array(array_p, 8);
    //lets modify the array
      pass_by_reference(array_p, 8);
      printf("After modification\n");
      print_1D_array(array_p, 8);
    //lets print the adresses of the array elements
      printf("Addresses of elements of array \n");
      print_1D_array_adress(array, 8);
    }
    
    void pass_by_reference(int *pointer, int N)
    {
      for (int i = 0; i < N; i++) {
        *(pointer + i) += 3;
      }
    }
    
    void print_1D_array(int *array, int N)
    {
      for (int i = 0; i < N; i++) {
        printf("%d ", *(array + i));
      }
      printf("\n");
    }
    
    void print_1D_array_adress(int *array, int N)
    {
      for (int i = 0; i < N; i++) {
        printf("%p\n", array + i);
      }
      printf("\n");
    }
    As for the rest, start by changing int* to char *
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2019
    Posts
    7
    You didn't do anything on the code,its the same.Read the question well please.İt says replace vowels with character '1' and consonants with '0'.This it what i couldn't do.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well I certainly wasn't going to spoon-feed you the complete answer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2019
    Posts
    7
    I tried,but i couldn't. And if i knew the answer I wouldn't ask it right. Want help to complete it and i don't have much time ,i need to send it.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You did get help to complete it: "indent your code", "start by changing int* to char *"

    The instructions are pretty clear about the point #1, but you don't seem to have even done that yet.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2019
    Posts
    7
    İ understand the #1 question ,my real question is the second one man, the code above it is the original one.Not my code.In my code i already done the first. i want you to write code about the second question vowels and consonants i don't know how to change them into character '1' and '0'.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Maybe begin by writing an isVowel() function.

    Code:
    int isVowel(char c) {
      int result = 0;  // start off pessimistic
      if ( /* some tests on c */ ) {
        result = 1;  // qaplah!
      }
      return result;
    }
    Then you can loop through the characters testing each one, and decide whether to replace it with '1' or '0'.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Dec 2019
    Posts
    7
    You dont understand me man i am not gonna write anymore u are not helping me.İ am saying you ı cant do it and i dont have time need to send it,but you dont understand write the full code or dont write anything.I am waiting here for 2 days.

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by mkoruk View Post
    You dont understand me man i am not gonna write anymore u are not helping me.İ am saying you ı cant do it and i dont have time need to send it,but you dont understand write the full code or dont write anything.I am waiting here for 2 days.
    You are a impolite ignorant person; I suggest you leave this website.
    It is for people who wish to learn how to program. You appear to have no desire to learn anything.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    We're not here to just hand you the code just because you're too lazy/busy/stupid to do it yourself.

    Last hint.
    Code:
    void pass_by_reference(char *pointer, int N)
    {
      for (int i = 0; i < N; i++) {
        if ( isVowel(pointer[i]) ) {
            pointer[i] = '1';
        } else {
            pointer[i] = '0';
        }
      }
    }
    If you want some more actual help, then YOU need to start posting code, not pleading like some kind of special snowflake.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mkoruk View Post
    İ am saying you ı cant do it and i dont have time need to send it,but you dont understand write the full code or dont write anything.I am waiting here for 2 days.
    Unfortunately, that means that you deserve to fail this assignment. What you can do for your next assignment would be to start earlier so you have time to actually give it a go instead of just giving up and hoping some random person online will do your assignment for you.

    If you do run into problems after putting in the effort, that's when you could come here and ask for help.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help pls for my c code homework
    By elitrex in forum C Programming
    Replies: 4
    Last Post: 11-14-2012, 08:25 AM
  2. Need help with this code for homework!
    By ch147 in forum C Programming
    Replies: 7
    Last Post: 11-13-2007, 08:16 AM
  3. need help with homework code
    By acidbeat311 in forum C Programming
    Replies: 2
    Last Post: 02-15-2005, 10:43 PM
  4. homework code/looking for suggestions
    By m.albert in forum C++ Programming
    Replies: 3
    Last Post: 03-03-2003, 03:50 PM

Tags for this Thread