Thread: Having the output of a While loop feed directly into an array

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    4

    Having the output of a While loop feed directly into an array

    The below program (that I didn't write) prints to the screen the first 100 prime numbers. I am tiring to get it to put the 100 prime numbers into an array.

    Code:
     
    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
    int p, number;
    int count=0;
    
       p=2;
    
       while (count < 100) {
    
          number=2;
          while (number < p) {
             if (p % number == 0) {
    
                number = p;
             }
             number=number+1;
          }
    
          if (number == p) {
             printf("%03d. prime number is %4d\n", count+1, p);
             count=count+1;
          }
          p=p+1;
       }
    
       return 0;
    }
    I started by defining an array "int prime [100];" and the printf function above and replaced it with "p=prime[]" then "p=prime[100]" , then "p=prime" and finally "prime=p", but it wouldn't work

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    I think you are going to want to read up on arrays a little more. What you want to do is very simple, and very achievable with some knowledge on arrays. If you have read a tutorial or something and you aren't sure about them, then ask what you aren't sure about instead.

    It's better to get a general understanding of the way something works than to just try to apply it in an example.

    There are plenty of guides on arrays, just google "C Arrays".

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Hey Kevin, welcome to the forum.

    You're like me - basically backwards, sometimes. Not:
    p=prime[100]

    but

    prime[count] = p;, right inside your if statement near the bottom of the program.

    And since this IS C:

    prime[count++] = p; //and you can delete the count = count + 1


    Make sure you have:
    int prime[100] = 0;

    in the code, right at the top of main();

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    4

    Thanks for the Help

    Adak and DeadPlanet,

    Thanks for the help.

    DeadPlanet,
    I need to do a lot more that just tutorial on arrrays. I have been doing a few, but it's been taking a couple of goes for the info to sink in.


    Thanks again
    Kevin

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop through a 2D array - just for select rows
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 04-17-2009, 08:34 AM
  2. array and loop help
    By hockey1 in forum C Programming
    Replies: 3
    Last Post: 03-26-2009, 09:47 PM
  3. Help with output from array
    By jaw24 in forum C++ Programming
    Replies: 4
    Last Post: 04-05-2007, 09:16 PM
  4. Ex. 2-4 KnR (2nd ed.) loop && array handling
    By olbas in forum C Programming
    Replies: 2
    Last Post: 03-23-2004, 11:07 PM
  5. Replies: 3
    Last Post: 02-19-2003, 08:34 PM