Stephenfrazier10's Profile User Rating: -----

Reputation: 2 Apprentice
Group:
Active Members
Active Posts:
109 (0.15 per day)
Joined:
12-September 12
Profile Views:
601
Last Active:
User is offline Oct 15 2013 10:43 AM
Currently:
Offline

Previous Fields

Country:
US
OS Preference:
Windows
Favorite Browser:
FireFox
Favorite Processor:
Intel
Favorite Gaming Platform:
XBox
Your Car:
Ford
Dream Kudos:
0

Latest Visitors

Icon   Stephenfrazier10 has not set their status

Posts I've Made

  1. In Topic: How to create a specific array using malloc in C?

    Posted 15 Oct 2013

    I was attempting to follow a tutorial, but that was not working, so here is my coded version of it. This makes a lot more sense to me.

    /* Include all of the needed files */
    #include <math.h>
    #include <stdio.h> 
    #include <stdlib.h>
    
    int main()
    {
        int* array;
        int MAX;
        printf("Find primes up to: ");
        scanf("%d", MAX);
        /* Ensure MAX is greater than 2 */
        while(MAX < 2)
        {
            /* Repeat Process if MAX is less than 2 */
            printf("HAS to be greater than 2! \n");
            printf("Find primes up to: ");
            scanf("%d", MAX);
        }
        /* Create the array with the amount of spaces chosen by the user */
        array = malloc(MAX * sizeof(int));
        
        int start = 2;
        /* Fill the array up to the number specified by the user */
        for(start = 2; start <= MAX - 1; start++)
        {
            array[start] = 1;
        }
        
        int y;
        /* Go through and fill the multiples of 1-9 with 0's */
        for(y = 2; y < 10; y++)
        {
            int x;
            for(x = y; x <= MAX; x+=y)
            {
                array[x] = 0;
            }
        }
    
    
  2. In Topic: How to create a specific array using malloc in C?

    Posted 15 Oct 2013

    Okay so I have been working on it for a while now, and think Im nearly done. The final program is supposed to ask the user how high they want to go, make sure its at least 2, create an array using malloc, fill the array with the numbers 2 through whatever the user entered, use the sieve of eratosthenes to find the primes, and then print out the primes between 2 and the number given by the user. My compiler will not stop loading, does anyone get an error message from this or see something Im doing wrong?

    /* Include all of the needed files */
    #include <math.h>
    #include <stdio.h> 
    #include <stdlib.h>
    
    int main()
    {
        int* array;
        int MAX;
        int z = 1;
        int j;
        printf("Find primes up to: ");
        scanf("%d", MAX);
        /* Ensure MAX is greater than 2 */
        while(MAX < 2)
        {
            /* Repeat Process if MAX is less than 2 */
            printf("HAS to be greater than 2! \n");
            printf("Find primes up to: ");
            scanf("%d", MAX);
        }
        /* Create the array with the amount of spaces chosen by the user */
        array = malloc(MAX * sizeof(int));
        
        int start = 2;
        /* Fill the array up to the number specified by the user */
        for(start = 2; start <= MAX - 1; start++)
        {
            array[start] = start;
        }
        
        int i;
        for (i = 2;i < MAX; i++)
            array[i] = 1;
    
        for (i = 2;i < MAX; i++)
            if (array[i])
                for (j = i ;i * j < MAX; j++)
                    array[i * j]=0;
    
        for (i = 2; i < MAX; i++)
            if (array[i])
                printf("%dth prime = %dn",z++,i);
    }
    
    
  3. In Topic: How to create a specific array using malloc in C?

    Posted 14 Oct 2013

    View Postjimblumberg, on 14 October 2013 - 11:29 PM, said:

    Quote

    How would I start to fill the array with the numbers 2 through the number specified by the user?

    Only if you want to access your array out of bounds. Arrays in C/C++ start at zero and stop at size - 1. So when you declared your array size MAX using <= MAX will access your array out of bounds.

    Jim


    That was really confusing, but it kind of made sense. Is there a way to fill the array without using the [] brackets? Thats what the book actually says to do?
  4. In Topic: How to create a specific array using malloc in C?

    Posted 14 Oct 2013

    How would I start to fill the array with the numbers 2 through the number specified by the user? Would this do it? Or is there a better way to do it with malloc?

    int start = 2;
        /* Fill the array up to the number specified by the user */
        for(start = 2; start <= MAX; start++)
        {
            array[start] = start;
        }
    
    
  5. In Topic: How to create a specific array using malloc in C?

    Posted 14 Oct 2013

    Wow! Thanks, I always miss the smallest things.

My Information

Member Title:
D.I.C Head
Age:
20 years old
Birthday:
January 21, 1994
Gender:
Location:
VA
Interests:
MMA, Politics, Computer Science, gaming
Full Name:
Stephen Frazier
Years Programming:
2
Programming Languages:
Python, Java, HTML

Contact Information

E-mail:
Click here to e-mail me
MSN:
MSN  stephenfrazier@go.rmc.edu
Yahoo:
Yahoo  stephenfrazier10@yahoo.com

Friends

Stephenfrazier10 hasn't added any friends yet.

Comments

Stephenfrazier10 has no profile comments yet. Why not say hello?