Question

In: Computer Science

#include <stdio.h> #include <stdlib.h> #include <time.h> void sort(int a[], int size); void printArray(int a[], int size);...

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

void sort(int a[], int size);
void printArray(int a[], int size);

int main(){
        int arraySize, limit, count,

        srand(time(0)); 

        print f("Enter the size of array\n");
        scanf("%d", arraySize);

        int array[arraySize];

        printf("Enter the upper limit\n");
        scanf("%d", &limit);

        count = 0;
        while(count <= arraySize){
                array[count] = (rand() % (limit + 1));
                count++;
        }

        printArray(array, &arraySize);

        sort(array, arraySize);

        printArray(array, arraySize);

        Return 0;
}

void printArray(int a[], int size){
        int i = 0;
        printf("Array: [");
        while(i < size){
                if(i != size - 1){
                        printf("%d, ", a[i]);
                } else {
                        printf("%d]\n", a[i]);
                }
                printf("%d, ", a[i]);
        }
}

Find the bugs in the given code and write the correct line of code where the bug is. There are a total of 7 bugs.

Solutions

Expert Solution

Going from the top :

Bug 1 : int arraySize, limit, count,   => This should end with a semi-colon

Bug 2 : scanf("%d", arraySize); => "&" is missing in arraySize

Bug 3: while(count <= arraySize) => Because count starts from 0 , it shoul have "<" instead of "<="

Bug 4 : printArray(array, &arraySize); => According to the declaration of the func, it should not have "&"

Bug 5 : sort(array, arraySize); => Definition for the sort func is missing

Bug 6 : Return 0; => R should be small i.e "return 0"

Bug 7 : printf("%d, ", a[i]); in printArray func => instead of this it should have a "i++" .

Here's the working code :

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

void sort(int a[], int size);
void printArray(int a[], int size);
void swap(int*,int*);

int main(){
int arraySize, limit, count; //Bug1

srand(time(0));

printf("Enter the size of array\n");
scanf("%d",&arraySize);//Bug2

int array[arraySize];

printf("Enter the upper limit\n");
scanf("%d", &limit);

count = 0;
while(count < arraySize){//Bug3
array[count] = (rand() % (limit + 1));
count++;
}

printArray(array, arraySize);//Bug4
sort(array,arraySize);//Bug5

printArray(array, arraySize);

return 0;//Bug6
}

void printArray(int a[], int size){
int i = 0;
printf("Array: [");
while(i < size){
if(i != size - 1){
printf("%d, ", a[i]);
} else {
printf("%d]\n", a[i]);
}
i++;//Bug7
}
}
sort(int a[],int size){
for(int i=0;i<size-1;i++)
{
for(int j=0;j<size-i-1;j++){
if(a[j]>a[j+1]){
swap(&a[j],&a[j+1]);
}
}
}
}
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

Output:


Related Solutions

Please paraphrase this c code #include <stdio.h> #include <stdlib.h> #include <string.h> void sortGrades(int arr[], int size,...
Please paraphrase this c code #include <stdio.h> #include <stdlib.h> #include <string.h> void sortGrades(int arr[], int size, int status, char names[][20]); void printer(int grades[], int size, char names[][20]); void sortNames(char arr[][20], int size, int status, int grades[]); void nameSearch(int grades[], int size, char names[][20]); void numSearch(int grades[], int size, char names[][20]); int main() { int i; int size; int option; do { printf("\n\nInput Number of Students or 0 to exit : "); scanf("%d", &size); if (size == 0) { break; }...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t player1, player2, player3;     pthread_create(&player1, NULL, (void *)race, NULL);     pthread_create(&player2, NULL, (void *)race, NULL);     pthread_create(&player3, NULL, (void *)race, NULL);     pthread_join(player1, NULL);     pthread_join(player2, NULL);     pthread_join(player3, NULL);     printf("Total Number = %d\n", shared);     return 0; } void race(void) {     long i,tmp;     for(i=1; i<=200000; i++) {         tmp = shared;         tmp = tmp + 1;         shared =...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid;...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid; // Main process's PID=42 pid = fork(); // creates process with PID=36 if (pid == 0) { pid_t pid2 = fork(); // creates process with PID=99 sleep(10); if (pid2 > 0) { sleep(10); exit(0); } else { sleep(30); printf("** ONE **\n"); exit(0); } } else { pid_t pid3 = fork(); // creates process with PID=71 if (pid3 == 0) { sleep(30); exit(0); } pid_t...
#include <stdlib.h> #include <stdio.h> #include <string.h> void clrScreen(int lines){     int i = 0;     for( i =...
#include <stdlib.h> #include <stdio.h> #include <string.h> void clrScreen(int lines){     int i = 0;     for( i = 0; i < lines; ++i ){         printf("\n");     }     return; } void printRules(void){     printf("\t|*~*~*~*~*~*~*~*~*~ How to Play ~*~*~*~*~*~*~*~*~*~|\n");     printf("\t|   This is a 2 player game. Player 1 enters the   |\n");     printf("\t|   word player 2 has to guess. Player 2 gets a    |\n");     printf("\t|   number of guesses equal to twice the number    |\n");     printf("\t|   of characters. EX: If the word is 'example'    |\n");     printf("\t|   player 2 gets 14 guesses.                      |\n");     printf("\t|*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~|\n");     clrScreen(10);     return; } //------------------------------------------------------------------------------------------------------------ /*...
#include <stdio.h> #include <stdlib.h> // required for atoi int main(void) {     int i=0,n,num,filenum[100],pos;     int...
#include <stdio.h> #include <stdlib.h> // required for atoi int main(void) {     int i=0,n,num,filenum[100],pos;     int c;    char line[100]; //declaring string for storing data in the line of text    FILE *fp; // declaring a FILE pointer    fp=fopen("numbers.txt","r"); // open a text file for reading    while(fgets(line, sizeof line, fp)!=NULL) {       // looping until end of the file         filenum[i]=atoi(line); //converting data in the line to integer and storing it into array        i++;    }...
Use C language , pointer limit use //#include <stdio.h> //#include <stdlib.h> //#include <time.h> For example, I...
Use C language , pointer limit use //#include <stdio.h> //#include <stdlib.h> //#include <time.h> For example, I have random array [4,2,7,1,9,8,0]. Sort the array's index but cannot change the position of item in the array, if you copy the array to a new array, you also cannot change the item's position in array. The index now is[0,1,2,3,4,5,6] The output should be[6,3,1,0,2,5,4]
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the...
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the computer won, 2 if there is a tie, and -1 if the player decides to quit int menu(int *); // Displays choices to user // Receives score array int main() { srand(42); // Seeding Random with 42 int score[3]; // Array keeping Player, Computer, and Tie Scores score [0] = 0; // Player - initialized to Zero score [1] = 0; // Computer -...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) { fprintf(stderr,"Usage: %s <integer>\n", argv[0]); exit(1); } pid_t pid1, pid2; while (count > 0) { pid1 = fork(); if (pid1 > 0) { pid2 = fork(); count = count - 2; } else if (pid1 == 0) { count = count - 1; } } exit(0); } Question #1 [2 pts] If the command-line argument passed to this...
#include "pch.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main() {        FILE...
#include "pch.h" #include <iostream> #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main() {        FILE *fp;        char c;        errno_t err;        err = 0;        err = fopen_s(&fp,"Desktop/test.txt", "r"); file is on my desktop but I err=2 but I don't think it is opening the file?        printf("%d\n", err);        if (err == 2)        {            printf("The file was opened\n");            while (1)       ...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT