Thread: Small problem with printing elements of an array

  1. #1
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34

    Small problem with printing elements of an array

    OK I'm horrible at this so bear with me. The user is supposed to enter a bunch of numbers that get stored in an array. Then it uses a function to print the elements of the array. I don't knwo how to set up the function parameters or whatever to get it to work but i'm pretty sure it should work other than that. Here's what I've got:

    Code:
    #include <stdio.h>
    
    
    void printset(int [], int);
    
    int main()
    {
    	int i = 0, a[25] = {0};
    
    	printf("elements in array are:\n");
    	for(i=0;i<25;i++)
    	{
    		scanf("%d",&a[i]);
    		 
    
    	
    	}
    printset();
    return 0;
    
    }
    	void printset(int b[25], int i)
    	{int i;
     for (i=0;i<25;i++)
     {
    
    	printf("{%d, ",b[i]);
    	printf(" }\n");
    
    	
    	
    	}
    	
    	
    }

  2. #2
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    well i think i figured it out, but now i need some more help. This is it so far.
    Code:
    #include <stdio.h>
    
    
    void printset(int [] );
    
    int main()
    {
    	int i = 0, a[25] = {0};
    
    	printf("elements in array are:\n");
    	for(i=0;i<25;i++)
    	{
    		scanf("%d",&a[i]);
    		 
    
    	
    	}
    printset(a);
    return 0;
    
    }
    	void printset(int a[])
    	{int i;
    	printf("{");
     for (i=0;i<25;i++)
     {
    
    	printf("%d, ",a[i]);
    
    
    	
    	
    	}
    	
    		printf(" }\n");
    }
    now how would i go about making it so the user doesn't HAVE to enter 25 numbers...just somewhere between 0 and 25?

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    Your formatting is horrible

    Well, you could ask the user to enter a negative number when he or she is done inputing the other numbers.

  4. #4
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    Quote Originally Posted by tcpl
    Your formatting is horrible

    Well, you could ask the user to enter a negative number when he or she is done inputing the other numbers.
    i'll do that

    i know my formatting is horrible this is just a rough draft i'm trying to get done quickly....I don't really care. Now my computer decided to crash and visual C++ is being worthless and i can't use it anymore so i'm screwed since my assignment is due in 15 minutes.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    umm

    ok, maybe im just missing it, but is there anywhere in this code where youre acutally having your user enter numbers? I could just be overlooking it, but...

  6. #6
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    Quote Originally Posted by xixpsychoxix
    ok, maybe im just missing it, but is there anywhere in this code where youre acutally having your user enter numbers? I could just be overlooking it, but...
    Code:
    	for(i=0;i<5;i++)
    	{
    		scanf("%d",&a[i]);
    		 
    
    	
    	}

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    yep

    I was definitely overlooking it... sry bout that... ill tell you if i see anything

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    ok...

    are you still using the code that you had above?

    Code:
    #include <stdio.h>
    
    
    void printset(int [] );
    
    int main()
    {
    	int i = 0, a[25] = {0};
    
    	printf("elements in array are:\n");
    	for(i=0;i<25;i++)
    	{
    		scanf("%d",&a[i]);
    		 
    
    	
    	}
    printset(a);
    return 0;
    
    }
    	void printset(int a[])
    	{int i;
    	printf("{");
     for (i=0;i<25;i++)
     {
    
    	printf("%d, ",a[i]);
    
    
    	
    	
    	}
    	
    		printf(" }\n");
    }
    or did you rewrite it for your negative number thing?

  9. #9
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    Quote Originally Posted by xixpsychoxix
    are you still using the code that you had above?

    Code:
    #include <stdio.h>
    
    
    void printset(int [] );
    
    int main()
    {
    	int i = 0, a[25] = {0};
    
    	printf("elements in array are:\n");
    	for(i=0;i<25;i++)
    	{
    		scanf("%d",&a[i]);
    		 
    
    	
    	}
    printset(a);
    return 0;
    
    }
    	void printset(int a[])
    	{int i;
    	printf("{");
     for (i=0;i<25;i++)
     {
    
    	printf("%d, ",a[i]);
    
    
    	
    	
    	}
    	
    		printf(" }\n");
    }
    or did you rewrite it for your negative number thing?

    well i have it modified to this now:

    Code:
    #include <stdio.h>
    
    
    void printset(int [] );
    
    int main()
    {
    	int i = 0, j = 0, a[5] = {0}, b[5] = {0};
    
    	printf("Choose some numbers for set 1:\n");
    	for(i=0;i<5;i++)
    	{
    		scanf("%d",&a[i]);
    		 
    
    	
    	}
    
    	printf("Choose some numbers for set 2:\n");
    	for(j=0;j<5;j++)
    	{
    		scanf("%d",&b[j]);
    		 
    
    	
    	}
    
    
    
    
    printf("Elements of set 1 are: \n");
    printset(a);
    
    printf("Elements of set 2 are: \n");
    printset(b);
    return 0;
    
    }
    	void printset(int c[])
    	{int k;
    	printf("{");
    	k = 0;
     for (k=0;k<5;k++)
     {
    
    	printf("%d, ",c[k]);
    
    
    	
    	
    	}
    	
    		printf(" }\n");
    }
    i'm trying to figure out how to do the negative number thing something in the for loop such as if (i = -1)
    break;

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    well..

    you can try this:

    Code:
    for(i=0;i<25;i++)
     {
      scanf("%d",&a[i]);
      if (a[i] < 0)
      {
       i = 25;
      }	
     }	
     printset(a);
     return 0;
    im not sure if thats what you wanna do, but you can give it a try

  11. #11
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    Quote Originally Posted by xixpsychoxix
    you can try this:

    Code:
    for(i=0;i<25;i++)
     {
      scanf("%d",&a[i]);
      if (a[i] < 0)
      {
       i = 25;
      }	
     }	
     printset(a);
     return 0;
    im not sure if thats what you wanna do, but you can give it a try
    that should work for now thanks!

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    150

    no prob...

    i'll keep looking at this code... so, from my understanding, you want to make it so that a user can terminate a loop by entering a certain character or value. why not make it a constant value?

  13. #13
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    Quote Originally Posted by xixpsychoxix
    i'll keep looking at this code... so, from my understanding, you want to make it so that a user can terminate a loop by entering a certain character or value. why not make it a constant value?
    actually yeah i'd rather make it terminate with -1, but any negative number will work. This is my current problem:

    if a 25 number array is terminated at say 5 numbers, the last 20 it will print is
    Code:
    {,0,0,0,0....etc}
    but i just want it to print the numbers the are actually entered.

  14. #14
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    how about this:?

    Code:
    	void printset(int c[])
    	{
    		int k;
    	printf("{");
    	k = 0;
     for (k=0;k<25;k++)
     {
    	if(c[k] != 0)
    	printf("%d, ",c[k]);
    
    
    	
    	
    	}
    	
    		printf(" }\n");
    }

  15. #15
    I am Diamond Dave
    Join Date
    Mar 2006
    Location
    You'll get some leg tonight for SURE!
    Posts
    34
    lol man my format is so horrible.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 05:37 AM
  2. problem copy from array to another
    By s-men in forum C Programming
    Replies: 3
    Last Post: 09-07-2007, 01:51 PM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. C Filling and Printing a 2-d array
    By Smoot in forum C Programming
    Replies: 3
    Last Post: 11-13-2003, 08:42 PM
  5. Replies: 2
    Last Post: 08-03-2003, 10:01 AM