Array help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • breezy79
    New Member
    • Nov 2008
    • 5

    Array help

    I'm having trouble with an array function involving seed. The code needs to display the array size in the generate function, if someone could glance at my code to point me in the right direction, I would be much obliged.
    [CODE]
    #include<stdio. h>
    #include<conio. h>
    #include<math.h >
    #include<stdlib .h>
    #define SIZE 100
    void generate(int[],int,int);
    void sort(int[],int);
    void mean(int[],int);
    void median(int[],int);
    void mode(int[],int);
    void variance(int[],int,int);
    void sd(int);
    void main()
    {
    int a[SIZE];
    int n, seed;
    printf("Please enter seed");
    scanf_s("%d", &seed);
    while(seed>0)
    {
    printf("Please enter array size");
    scanf_s("%d", &n);
    while(n<=1||n<= 100)
    {
    generate(a,seed ,n);
    //sort(a,n);
    //mean(a,n);
    //median(a,n);
    //mode(a,n);
    //variance(a,n,m) ;
    //sd(v);
    printf("please enter array size");
    scanf_s("%d", &n);
    printf("Please enter seed");
    scanf_s("%d", &seed);
    }
    }
    }
    void generate(int x[], int seed, int n)
    {
    int i, srand;
    srand(seed);
    for(i=0;i<n;i++ )
    {
    x[i]=rand()%10;
    printf("%d", x[i]);
    }
    }
    [CODE]
  • vmpstr
    New Member
    • Nov 2008
    • 63

    #2
    Code:
    while(n<=1||n<=100)
    that line is equivalent to

    Code:
    while(n <= 100)
    you probably want to change one of the signs to "greater than or equal to"

    If you want to print the array size, just use printf("%d", n); in your function...

    You are probably trying to ask something else... Try rephrasing your question.

    Comment

    • breezy79
      New Member
      • Nov 2008
      • 5

      #3
      Sorry, it says,the user enters a non-zero number as seed and a positive integer, say n, which is the size of the sample. "n" has to be validated between 1 and 100. Ask for another n if n is not valid. Terminate the program if seed is zero.

      Comment

      • breezy79
        New Member
        • Nov 2008
        • 5

        #4
        Then it says generate n integers and use the remainder operator to have all these numbers become no greater then 10.

        Comment

        • oler1s
          Recognized Expert Contributor
          • Aug 2007
          • 671

          #5
          And what has compelled you to post that information here? We don't hand out free code, so you know to focus your question.

          Comment

          • breezy79
            New Member
            • Nov 2008
            • 5

            #6
            My code is the first post I'm just asking for help.

            Comment

            • vmpstr
              New Member
              • Nov 2008
              • 63

              #7
              Well, you are pretty much there. the condition on the while loop that I meant has to be fixed.

              You need the loop to continue while n >= 1 and n <= 100. What you have is the loop continues while n <= 1 or n <= 100. If n <= 1, then surely it's also <= 100. You need a logical and there, not or, and the greater than sign.

              You should also terminate the program if n is the the right range, but the seed is 0. You can do this by adding a check at the end of the inner-most loop.

              It would also help the user if you printed "n is not in the valid range" or something to that effect if n < 1 or n > 100.

              fix those things up and see if it helps.

              good luck

              Comment

              • breezy79
                New Member
                • Nov 2008
                • 5

                #8
                Thank you so much vmpstr.

                Comment

                Working...