Question

In: Computer Science

hi! I have this code. when I run it, it works but in the print(the number...

hi! I have this code. when I run it, it works but in the print(the number we are searching for is ) it prints the number twice. ex. for 5 it prints 55 etc. apart from this, it works

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

int main(){
int n,i;
printf("Give me the size of the table : \n");
scanf("%d", &n);
int A[n],x,y;

for (i=0;i<n;i++)
A[i]=rand() % n;

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

srand(time(NULL));
y=rand()% n ;

printf("The number we are searching for is %d", y);

for (i=0;i<n;i++)
{
    if (A[i]==y)
    {
      printf("%d is present at location %d.\n", y,i+1);
      break;
    }
}
if (i == n)
    printf("%d isn't present in the array.\n", y);

return 0;
}

Solutions

Expert Solution

The code is working fine. but displaying two times of the number searching so that it is displaying two times.

please take a look on the code corrected code:

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

int main(){
   int n,i;
   srand(time(0));
   printf("Give me the size of the table : \n");
   scanf("%d", &n);
   int A[n];
   int x,y;
  
   for (i=0;i<n;i++)
       A[i] = rand()%n;
  
   for (i=0;i<n;i++)
       printf("%d\n", A[i]);

   srand(time(NULL));
   y=rand()%n; //random generate number upto n numbers
   //do not have to display here the y value if display then
   //not display in the inside for loop in print statement
   printf("The number we are searching for is ");
   for (i=0;i<n;i++) //loop to the n times
   {
        if (A[i]==y) //check for the index value is same as y value
        {
          //here is printed again the y value so that it is displaying two times the value corrected
          printf("%d is present at location %d.\n", y,i+1); //prints the found with the index
          break; //breaks the loop
        }
   }
   if (i == n) //if not found
        printf("%d isn't present in the array.\n", y);
  
   return 0;
}

sample output:


Related Solutions

HI. I have been trying to run my code but I keep getting the following error....
HI. I have been trying to run my code but I keep getting the following error. I can't figure out what I'm doing wrong. I also tried to use else if to run the area of the other shapes but it gave me an error and I created the private method. Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at project2.areacalculation.main(areacalculation.java:26) My code is below package project2; import java.util.Scanner; public class areacalculation { private static...
Hi, I have created the following code and I was wondering if it is possible to...
Hi, I have created the following code and I was wondering if it is possible to make an "if" statement in the first for loop that would output an error if the user enters a float, character or string? I was thinking of something along the lines of: if(a[i] != int) but that didn't work :( Thank you. #include <iostream> using namespace std; // Creating a constant for the number of integers in the array const int size = 10;...
Hi I have a java code for my assignment and I have problem with one of...
Hi I have a java code for my assignment and I have problem with one of my methods(slice).the error is Exception in thread "main" java.lang.StackOverflowError Slice method spec: Method Name: slice Return Type: Tuple (with proper generics) Method Parameters: Start (inclusive) and stop (exclusive) indexes. Both of these parameters are "Integer" types (not "int" types). Like "get" above, indexes may be positive or negative. Indexes may be null. Description: Positive indexes work in the normal way Negative indexes are described...
Your code must print all the steps in the output as an Eg> When you run...
Your code must print all the steps in the output as an Eg> When you run your Merge sort code the sequence of output should be: 1) Enter input sequence 2) Show n/2 division of the input sequence 3) keep showing n/2 division of your sequence until you get one attribute 4) Sort first two numbers 5) Make a stack of 4 by merging 2 * 2 and sort them 6) keep showing merging and sorting until you show the...
(Python) This is my code for printing a roster for a team. When I print to...
(Python) This is my code for printing a roster for a team. When I print to the console, it makes the first player's name show up as number 2, and it says [] (its just blank for 1). How can I fix that so the first player's name is 1, not skipping 1 and going to 2. def file_to_dictionary(rosterFile): myDictionary={} myDict=[]    with open(rosterFile,'r') as f: for line in f:    (num,first,last,position)=line.split() myDictionary[num]= myDict myDict=[first, last, position] print (myDictionary) return...
I have a code and it works and runs as it supposed too. What is the...
I have a code and it works and runs as it supposed too. What is the UML for it? Any help will be awesome. Thanks. import java.util.Scanner; public class StringToMorseCode { public static void main(String[] args){ Scanner input = new Scanner(System.in);    char[] letters = { ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6',...
Here is my fibonacci code using pthreads. When I run the code, I am asked for...
Here is my fibonacci code using pthreads. When I run the code, I am asked for a number; however, when I enter in a number, I get my error message of "invalid character." ALSO, if I enter "55" as a number, my code automatically terminates to 0. I copied my code below. PLEASE HELP WITH THE ERROR!!! #include #include #include #include #include int shared_data[10000]; void *fibonacci_thread(void* params); void parent(int* numbers); int main() {    int numbers = 0; //user input....
1. WHEN I RUN THE CODE AT FIRST AND I SELECT THE RETURN OPTION, IT BREAKS....
1. WHEN I RUN THE CODE AT FIRST AND I SELECT THE RETURN OPTION, IT BREAKS. BUT WHEN I SELCT THE RETURN OPTION AFTER I BORROW A BOOK, IT WORKS 2. WHY IS MY ARRAYLIST NO SAVING? WHAT I MEAN IS THAT AFTER I BORROW A BOOK AND I END THE SYSTEM, WHEN I RESTART IT, THE BOOK I BORROWED IS STILL AVAILABLE 3.ALSO ANY IDEAS ON WHAT MY TEST CASES COULD BE? /* * To change this license header,...
I can't get the number of days to print. Here is my code: public static void...
I can't get the number of days to print. Here is my code: public static void main(String[] args) { // Prompt the user to enter year Scanner scanner = new Scanner(System.in); // Prompt the user to enter year System.out.print("Enter full year (e.g., 2016): "); int year = scanner.nextInt(); for(int i = 1; i <= 12; i++) printMonth(year, i); } /** Print the calendar for a month in a year */ static void printMonth(int year, int month) { // Print the...
Hi, I have this code so far and need to modify it so that the output...
Hi, I have this code so far and need to modify it so that the output does not print something like 2x^0 but instead will just print 2. Currently it prints 2x^0. I also am having a problem with the output of Polynomial 1 - Polynomial 2. If both coefficient values for the polynomials are equal instead of Polynomial 1 - Polynomial 2 = 0 it outputs nothing. Just Polynomial 1 - Polynomial 2 = For example if I input...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT