can u help me with pointers?plzzzzzzzz

This is a discussion on can u help me with pointers?plzzzzzzzz within the C Programming forums, part of the General Programming Boards category; Assignment #2 (Pointers) Problem (1) (Array of pointers to Functions that manipulate pointers) Use pointer operations only to manipulate the ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    14

    Unhappy can u help me with pointers?plzzzzzzzz

    Assignment #2 (Pointers)

    Problem (1) (Array of pointers to Functions that manipulate pointers)
    Use pointer operations only to manipulate the array elements.
    Use an array of pointers processNumbers where each element of the array points to one of the above functions.


    a) Write a function addElement that takes as parameters: a pointer to a sorted array of integer numbers, array size (number of elements stored in the array), and the number to be inserted in the array. The function should insert the element in the proper position to keep the list sorted. Use pointer operations to access the elements of the array. The function prototype is

    void addElement(intt *array, int *sizeptr, int num);



    b) Write a function deleteElement that takes as parameters: a pointer to a sorted array of integer numbers, array size (number of elements stored in the array), and the number to be removed from the array. The function should remove the element and keep the list sorted. The function prototype is

    void deleteElement(int *array, int *sizeptr, int num);



    c) Write a function printArray that takes as parameters: a pointer to array of int numbers and its size. The function should print the contents of the array. The function prototype is void printArray(int *array, int size);



    d) Write a function findMaximum that takes as parameters: an array of integer numbers and its size. The function should find the maximum number in the array. Use a suitable prototype.



    e) Write a program with a menu driven interface. The program should offer the user 4 options as follows:



    Enter a choice:

    0 Add a number to the list

    1 Remove a number from the list

    2 Print the array numbers

    3 Find Maximum

    4 End program








    Problem (2)
    a) Write a function findMaxPos that takes as parameters : a pointer which points to a two‑dimensional array array2dptr, number of rows, and number of columns. The function returns the position (row #, column#) of the maximum number in the 2d‑array.The address of any element in the 2d-array with array2dptr pointing to the first element in the array is array2dptr +rownum*colsize+colnum. The function prototype is int find2dTotal(int *, int, int);

    b) Write a main program to declare, store values in the 2d-array, and call the above function find2dTotals to display the total of adding the values of the cells.


    thanx
    show me the meaning

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    i dont think anyones going to do your assignment/homework for you. why dont you give it a try first then we can help
    there are only 10 people in the world, those who know binary and those who dont

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    14

    plz help

    I cant answer it cuz
    Iam studing for my mid semester exams
    so I dont have the time
    and if u want the truth
    I didnt under stand the pointers
    so i cant answer it
    if u gona explain to me
    I will be thankful

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    no, kurz7 is right.

    either pick a question and ask it(one you have, not homework), or show some code.

  5. #5
    Registered User codingmaster's Avatar
    Join Date
    Sep 2002
    Posts
    309
    First, I wanna say nobody wanna do the homework for somebody else, when this person is lazy to try to do them.

    So, I won't do your work. I wanna just to try to explain the work with pointers.....
    For Further information, read the www.cprogramming.com tutorials.....


    pointers
    ===================

    declaration syntax:

    Datatype *pointervariable

    Example:
    +++++++++++++

    int x=5;

    address: 0000:1234
    name: x
    value: 5

    code:
    Code:
    #include <stdio.h>
    
    int main()
    {
       int x=5;
       printf("The address of x is %p \n",&x);
       return 0;
    }
    Something about addresses.......

    code:
    Code:
    #include <stdio.h>
    
    int main()
    {
     int check;
     int chapter1 = 5;
     int chapter2 = 60;
     int chapter3 = 166;
     int *data;
    
     do{
         printf("\tChapter overview\n");
         printf("\t*******************************\n\n");
         printf("\t-1- Chapter 1\n");
         printf("\t-2- Chapter 2\n");
         printf("\t-3- Chapter 3\n");
         printf("\t-4- End\n");
         printf("\n");
         printf("\tOption: ");
         scanf("%d",&check);
         printf("\tYou find chapter %d on ",check);
    
         switch(check)
          {
           case 1 :  data=&chapter1;
                     printf("page %d\n",*data);
                     break;
           case 2 :  data=&chapter2;
                     printf("page %d\n",*data);
                     break;
           case 3 :  data=&chapter3;
                     printf("page %d\n",*data);
                     break;
           default:  printf("page ???\n");
                     break;
         }
       }
       while(check<4);
     return 0;
    }

    This is another example:

    Code:
    #include <stdio.h>
    
    int main()
    {
     int x=5;
     int *y;
    
     printf("address x=%p, value x=%d\n",&x,x);
     printf("address *y=%p, value *y=%d(crap)\n",&y,*y);
    
     printf("\ny=&x;\n\n");
     y=&x;              /*y has now the address of x */
    
     printf("address x=%p, value x=%d\n",&x,x);
     printf("address *y=%p, value *y=%d\n",&y,*y);
    
     printf("\ny points to this address %p\n",y);
     printf("this is the address of x = %p\n",&x);
    
     printf("\nWATCH OUT!!!\n\n");
    
     *y=10;
     printf("*y=10\n\n");
     printf("address x=%p, value x=%d\n",&x,x);
     printf("address *y=%p, value *y=%d\n",&y,*y);
    
     printf("\nypoints to this address %p\n",y);
     printf("it is still the address of x (%p)\n",&x);
    
     return 0;
    }

    The next example is imporatant. And never use such crap, it coul be very dangerous:
    Code:
    #include <stdio.h>
    
    int main()
    {
    int *y;
    *y=10;
    printf("the value of *y is %d\n",*y);
    return 0;
    }
    This is dangerous, because y points to a not defined address..........

    you could try to prevent such mistakes, when u use:
    Code:
    #define NULL (void *)0
    example:

    Code:
    #include <stdio.h>
    
    int main()
    {
    int *y=NULL;
    
    if(y==NULL)
    {
    printf("The pointer has an illegal/not defined address\n");
    }
    
    else
    {
    *y=10;
    }
    
    return 0;
    }

    Some other things to know:
    Code:
    int *ptr;    /* declaration */
    int var;
    
    ptr=&var;    /* initialistion : ptr get the address of var */
    *ptr=100;   /* dereferenciation: var get the value 100 */
    *ptr+=100; /* dereferenciation: we add 100 to var */
    *ptr++;    /* dereferenciation: var has now the value of 201 */
    *ptr--;    /* var has now the value of 200 again */
    printf("%d",*ptr);    /* outputs the value of var */
    printf("%p",&ptr);    /* outputs the address of ptr */
    printf("%p",ptr)      /* outputs the address of var */

  6. #6
    Registered User codingmaster's Avatar
    Join Date
    Sep 2002
    Posts
    309
    And the second part........


    pointers, which points onto other pointers

    example:
    Code:
    int *ptr1;
    int *ptr2;
    ptr1=ptr2;
    code:
    Code:
    #include <stdio.h>
    
    int main()
    {
     int value=10;
     int *ptr1;
     int *ptr2;
    
     ptr1=&value;
     ptr2=ptr1;
    
     printf("value=%d address in *ptr1=%p\n",*ptr1,ptr1);
     printf("address of *ptr1=%p\n",&ptr1);
    
     printf("value=%d address in *ptr2=%p\n",*ptr2,ptr2);
     printf("address of *ptr2=%p\n",&ptr2);
     printf("address of the int value=%p\n",&value);
     return 0;
    }

    Pointers in C/c++ have always to have the same datatypes as variables...........

    example, this code returns crap..... and good compilers like gcc would not compile the code and return an error, like cannot convert double* to int*:
    Code:
    #include <stdio.h>
    
    int main()
    {
      int *int_ptr;
      double double_value=999.999;
    
      int_ptr=&double_value;
      printf("*int_ptr=%d double=%f\n",*int_ptr, double_value);
      return 0;
    }

    Now, I gonna explain the usage of pointers as function parameters "Call-By-Reference"
    There is another method "call-by-value", which has often disadvanteges, because the compiler has always to copy the parameters, cause the call-by-value parameters are used as local variables in the functions.......

    example:
    Code:
    #include <stdio.h>
    #define PI 3.141592
    
    float circlearea(float value)
    {
       return (value = value * value * PI);
    }
    
    int main()
    {
       float r, area;
       printf("We calculate a circle area!!\n\n");
       printf("Input r : ");
       scanf("%f",&r);
    
       area = circlearea(r);
       printf("\nThe circle area: %f\n",area);
       return 0;
    }
    In such a case, we can easily use the address of r and pass the address of r as argument to the function.......

    call-by-reference example:
    Code:
    #include <stdio.h>
    #define PI 3.141592
    
    void circlearea(float *value)
    {
     *value = ( (*value) * (*value) * PI );
    }
    
    int main()
    {
       float r;
       printf("We calculate the circle area\n\n");
       printf("Input r: ");
       scanf("%f",&r);
       circlearea(&r);
       printf("\nThe circlearea : %f\n",r);
       return 0;
    }

  7. #7
    Registered User codingmaster's Avatar
    Join Date
    Sep 2002
    Posts
    309
    It think, that this part will be very interesting for you:

    ARRAYS and POINTERS

    First....
    Arrays and pointers have nothing in common. A pointer is the address of an address and an array name is only an address.

    Here is an example, how to access arrays with pointers:
    Code:
    #include <stdio.h>
    
    int main()
    {
     int element[8]= {1,2,4,8,16,32,64,128};
     int *ptr;
     int i;
    
     ptr=element;
    
     printf("*ptr points onto the value of %d\n",*ptr);
     printf("*ptr+1 points now onto the value of %d\n",*(ptr+1));
     printf("*(ptr+3) = %d\n",*(ptr+3));
    
     printf("\nAnd now a for loop: \n");
    
     for(i=0; i<8; i++)
     {
          printf("element[%d]=%d \n",i,*(ptr+i));
     }
     return 0;
    }

    When you use this:
    ptr=element
    You send the address of element to the pointer.
    This works without the address operator, cause the ANSI standard says, that the Array name is always like a pointer, which points onto the first array element.

    Here the example:
    Code:
    #include <stdio.h>
    
    int main()
    {
     int element[8] = {1,2,4,8,16,32,64,128};
     int i;
    
     printf("*element     = %d\n",*element);
     printf("*(element+1) = %d\n",*(element+1));
     printf("*(element+3) = %d\n",*(element+3));
    
     printf("\nNow a for loop: \n");
    
     for(i=0; i<8; i++)
     {
          printf("*(element+%d) = %d \n",i,*(element+i));
     }
     return 0;
    }
    When u wanna use the address operator, it's ok too.
    ptr=&element[0]; /* identical with ptr=element */

    Now another example, call-by-reference with arrays and pointers.....

    Code:
    #include <stdio.h>
    
    void function(int *array, int n_array)
    {
     int i;
     for(i=0; i < n_array; i++)
      printf("%d ",array[i]);
     printf("\n");
    }
    
    int main()
    {
     int values[] = { 1,2,3,5,8,13,21 };
    
     function(values, sizeof(values)/sizeof(int));
    
     return 0;
    }

    I hope, I have helped you.......
    ~codingmaster

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    14

    Wink

    THANK U
    I AM THANKFUL
    I WILL ANSWER IT
    AND POST IT HERE
    THANX
    U R BETTER THAN THE DOCTOR
    show me the meaning

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21