Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I made a C++ program that works except it\'s supposed to generate random numbers

ID: 3914841 • Letter: I

Question

I made a C++ program that works except it's supposed to generate random numbers in the 15-20 range and it doesn't. Here is my code:   

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

int main()
{   //variable declaration
     int arr[10],i,temp,j,sum=0;
    int find,count;
    float avg;
    char ch;
              
        srand(0);
        printf(" **Random Number array** ");
        for(i=0;i<10;i++)
        {
            arr[i]=14+rand()%10;    //Storing random number in an array
            printf("%d ",arr[i]);
        }
              
        while(1)
        {   //Displaying menu & taking choice from user
            printf(" P. Position R. Reverse A. Average S. Search Q. Quit Your Choice:");
            scanf(" %c",&ch);
            if(ch=='q' || ch=='Q') break;     
            switch (ch)
        {  
            case 'p': // This case will be executed when choice selected is 'P' or 'p'
            case 'P':
                        printf(" Array Element postion ");
                        for(i=0;i<10;i++)
                        printf("%d. %d ",i,arr[i]);

                        break;
                           
                        case 'r': // This case will be executed when choice selected is 'r' or 'R'
                        case 'R':
                        j=9;
                        i=0;
                        while(i<j)
                        {
                            temp=arr[i];
                            arr[i]=arr[j];
                            arr[j]=temp;
                            i++;
                            j--;
                        }
                            printf(" Reverse array ");
                            for(i=0;i<10;i++)
                            printf("%d ",arr[i]);
                           
                            break;
                                               
                            case 'a': // This case will be executed when choice selected is 'a' or 'A'
                            case 'A':
                           
                            printf(" Average of array is: ");
                            for(i=0;i<10;i++)
                            sum=sum+arr[i];
                                                                              
                            avg=(float)sum/10;
                            printf("%.2f ",avg);   //It will print average up to 2 decimal places
                           
                            break;
                           
                            case 's': // This case will be executed when choice selected is 's' or 'S'
                            case 'S':
                           
                                    count = 0;
                                    printf("Enter Number to search in array:");
                                    scanf("%d",&find);
                                    for(i=0;i<10;i++)
                                   
                                    {
                                        if(arr[i]==find)
                                    {
                                        printf("%d is present at location %d ",find,i);
                                        count++;
                                    }
                                    }
                                        if(count==0)
                                    printf("%d is not present in array ",find);
                                        else
                                    printf("%d is present %d times in array. ",find,count);
                                                                                break;   
                                }                              //End of switch
                }                              //End of while loop
return 0;
}   //End of main() function

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <iostream>

using namespace std;

int main()

{ //variable declaration

srand(time(NULL));

int arr[10],i,temp,j,sum=0;

int find,count;

float avg;

char ch;

  

srand(0);

printf(" **Random Number array** ");

for(i=0;i<10;i++)

{

arr[i]=15+(rand()%6); //Storing random number in an array

printf("%d ",arr[i]);

}

  

while(1)

{ //Displaying menu & taking choice from user

printf(" P. Position R. Reverse A. Average S. Search Q. Quit Your Choice:");

scanf(" %c",&ch);

if(ch=='q' || ch=='Q') break;

switch (ch)

{  

case 'p': // This case will be executed when choice selected is 'P' or 'p'

case 'P':

printf(" Array Element postion ");

for(i=0;i<10;i++)

printf("%d. %d ",i,arr[i]);

break;

case 'r': // This case will be executed when choice selected is 'r' or 'R'

case 'R':

j=9;

i=0;

while(i<j)

{

temp=arr[i];

arr[i]=arr[j];

arr[j]=temp;

i++;

j--;

}

printf(" Reverse array ");

for(i=0;i<10;i++)

printf("%d ",arr[i]);

break;

case 'a': // This case will be executed when choice selected is 'a' or 'A'

case 'A':

printf(" Average of array is: ");

for(i=0;i<10;i++)

sum=sum+arr[i];

  

avg=(float)sum/10;

printf("%.2f ",avg); //It will print average up to 2 decimal places

break;

case 's': // This case will be executed when choice selected is 's' or 'S'

case 'S':

count = 0;

printf("Enter Number to search in array:");

scanf("%d",&find);

for(i=0;i<10;i++)

{

if(arr[i]==find)

{

printf("%d is present at location %d ",find,i);

count++;

}

}

if(count==0)

printf("%d is not present in array ",find);

else

printf("%d is present %d times in array. ",find,count);

break;

} //End of switch

} //End of while loop

return 0;

} //End of main() function

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote