Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6876603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:29:53+00:00 2026-05-27T04:29:53+00:00

I am trying to determine why writing random values to an array is causing

  • 0

I am trying to determine why writing random values to an array is causing a problem.

I actually ask rand() to generate numbers between 1 and 10(rand() %10 +1, with srand(time(NULL)) before) and the first value is ALWAYS higher than 10: it’s a random one too, between 10 and 20. I really do not know how to fix that, as it looks like an issue with the rand and srand functions. Nevertheless this is my code:

Edit: correct code, now

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZEA 100
#define SIZEFREQ 10

int main()
{
    int a[SIZEA]={0},frequency[SIZEFREQ]={0};
    int i,temp,gothrough;

    srand(time(NULL));

    for(i=0;i<=SIZEA-1;i++)
    {
        a[i]=rand() %10 +1;
        ++frequency[a[i]-1];
    }

    printf("These are the elements in the vector:\n");
    for(i=0;i<=SIZEA-1;i++)
    {
        printf("%3d,",a[i]);
    }

    printf("\nLet's try to put them in order\n");
    for(gothrough=0;gothrough<=SIZEA-1;gothrough++)
    {
        for(i=0;i<=SIZEA-2;i++)
    {
        if (a[i]>a[i+1])
        {
            temp=a[i];
            a[i]=a[i+1];
            a[i+1]=temp;
        }
    }
}

for(i=0;i<=SIZEA-1;i++)
{
    printf("%3d,",a[i]);
}

printf("\n\nValue Frequency\n");
for(i=0;i<=SIZEFREQ-1;i++)
{
    printf("%5d%10d\n",i+1,frequency[i]);
}

return 0;
}`
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:29:54+00:00Added an answer on May 27, 2026 at 4:29 am

    The reason is simple.

    a[i] is between 1 and 10 and therefore when you write:

    ++frequency[a[i]+1];
    

    you are filling indices 2 to 11 of frequency. However, frequency has only indices 0 to 10. Therefore you are going over the array frequency and into the array a and writing over a[0]. This happens when a[i] is 10. Since with 100 numbers, there is 10% chance you get 10, you increment a[0] (by incrementing frequency[11]) about 10 times. Since the first value was also between 1 and 10, the final value gets between 10 and 20.

    Edit: For the same reason you index a from 0 to SIZE-1, you should also index frequency from 0 to 10. What you are doing is to create indices from 1 to 10, and also +1 them! For example this here:

    for(i=1;i<=10;i++)
        printf("%5d%10d\n",i,frequency[i+1]);
    

    should be

    for(i=0;i<10;i++)
        printf("%5d%10d\n",i,frequency[i]);
    

    Notice both starting from 0, not going to 10, and indexing frequency by i rather than i+1

    Alternatively, you could have

    for(i=1;i<=10;i++)
        printf("%5d%10d\n",i,frequency[i-1]);
    

    that indexes frequency by i-1 to make the index right.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a parser for CSV data, and am trying to determine how to
Trying to determine if it is possible to bind the SelectedValue of a ComboBox
Am trying to determine the best way to persist information from an originating email,
I trying to determine display vertical size of my Blackberry Storm 2. I know,
I'm trying to determine if there's a way in Visual Basic 2008 (Express edition
I am trying to determine the best time efficient algorithm to accomplish the task
I'm trying to determine, based on the result of this call, if it was
I'm trying to determine the best way to truncate or drop extra decimal places
I have been trying to determine a best case solution for registering a COM
I'm trying to determine if the user is using 24 hour or 12 hour

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.