Loop Help with Pointers.

This is a discussion on Loop Help with Pointers. within the C Programming forums, part of the General Programming Boards category; I am not sure where my problem is but here is the question asked of me and what I have ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 06: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, 03: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, 04: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, 09:15 AM

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