Thread: Loop Help with Pointers.

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    28

    Homework Help Pointers/Manipulation of array.

    I am not sure where my problem is but here is the question asked of me and what I have so far.

    I can get 2 to display however the rest seem to be non existant.

    # Write a C program that has a declaration in main() to store the following numbers into an array named channels: 2, 4, 5, 7, 9, 11, 13. There should be a function call to display() that accepts the channels as an argument named channels and then displays the numbers using the pointer notation *(channels + i).


    # Modify this
    display() function to alter the address in channels. Always use the expression *channels rather than *(channels + i) to retrieve the correct elements

    Code:
    #include <stdio.h>
    
    #define nums 7
    void display(int*,int);
    
    int main()
    {
    int x;   
    int channels[nums]={2,4,5,7,9,11,13};
    printf("Channels: ");
    
    void display(int *channels, int x);
    
    printf("%4d", *channels);
    
    return 0;
    }
    void display(int *channels,int x)
    {
         for(x=0; x<nums; x++)
         {
          *channels++;
                }
          }
    Last edited by 1BadRbt; 12-20-2006 at 01:26 AM. Reason: Narrowed down title to specific.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    void display(int *channels, int x);
    should be
    Code:
    display(channels,nums);
    for(x=0; x<nums; x++)
    should be
    Code:
    for(i=0;i<x;i++)
    *channels++;
    should be...
    read once more the assignement, it should include 2 things
    output function, for example printf and *(channels + i)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    28
    now i did part a of the assignment and unsure on part b.

    i rewrote code after watching a few videos

    here is the improved code.

    Code:
    #include <stdio.h>
    #define nums 7
    
    void display(int *);
    
    int main()
    {
        
    int channels[nums]={2,4,5,7,9,11,13};
    display(channels);
    }
    void display(int *channels)
    {
         int i;
         for(i=0;i<nums;i++)
         {
              printf("%5d",*(channels+i));
         }
         }
    Now I am stuck on manipulation of the display() to use *channels rather than *(channels+i) to retrieve the correct elements.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    28
    Nevermind here is the solution for all glad I helped myself, with some help from someone.

    Part A
    Code:
    #include <stdio.h>
    #define nums 7
    
    void display(int *);
    
    int main()
    {
        
    int channels[nums]={2,4,5,7,9,11,13};
    printf("Channels:");
    display(channels);
    }
    void display(int *channels)
    {
         int i;
         for(i=0;i<nums;i++)
         {
              printf("%5d",*(channels+i));
         }
         }
    Part B

    Code:
    #include <stdio.h>
    #define nums 7
    
    void display(int *);
    
    int main()
    {
        
    int channels[nums]={2,4,5,7,9,11,13};
    printf("Channels:");
    display(channels);
    }
    void display(int *channels)
    {
        int i;
         for(i=0;i<nums;i++)
         {
              printf("%5d",*channels);
              *channels++;
                  }
         }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  2. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  3. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM