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

//Error message: \"[Error] expected declaration specifiers or \'...\' before \'t

ID: 3731618 • Letter: #

Question

//Error message: "[Error] expected declaration specifiers or '...' before 'time'"

//My exact code is pasted below. I cant figure out why srand isnt working. Ignore what ive commented out as I havent started working on that yet.

#include "stdio.h"

#include "time.h"

#include "stdlib.h"

#define N 10

void print_array2(double array[], int length);

//void bubble_sort(double array[], int length, in order);

//void insertion_sort(double array[], int length);

int i;

srand(time(0));

void main(){

double array1[N];

double array2[N];

double array3[N];

for(i=0; i<N;i++){

array1[i] = rand() % 200 - 100;

array2[i] = rand() % 200 - 100;

array3[i] = rand() % 200 - 100;

}

print_array2(array1,N);

print_array2(array2,N);

print_array2(array3,N);

/*

bubble_sort(array1,N,0);

bubble_sort(array2,N,1);

insertion_sort(array3,N);

print_array2(array1,N);

print_array2(array2,N);

print_array2(array3,N);

*/

}

void print_array2(double array[], int length){

for(i=0; i<N;i++){

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

}

printf(" ");

}

CAUserslmjordDesktopC Homeworkic fileshw4_4.c - Dev-C+5.11 File Edit Search View Project Execute Tools AStyle Window Help Project Classes Debug globals) hw4 4.c 1 *3) Write a program that declares three arrays with N elements with random values between -100 to 100 2 3 4 I) rite a print_array2 function that prints the entires in an array of type double 5 6 7 8 VI) Compare the runtime of the bubble sort and the insertion sort functions (no submission required). 9 Use cr#define N 10" to set N to an arbitrary number before the main function. The define statement, the function prototypes, and the sample main function are provided below II) Write a bubble sort function that sorts the elements in ascending or descending order depending on the value of the input argument order (0-ascending and 1-descending) III) Write an insertion sort function that sorts that numbers in an array in ascending order. 10 #include "stdio.h" 11 #include "time.h" 12 #include "stdlib.h" 13 #define N10 14 15 void print_array2 (double array[], int length) 16 //void bubble-sort(double array[], nt Length, in order); 17 //void insertion_sort(double arrayl], int length) 18 int i; srand(dtime (e)); void main(){ 20 21 double array1[N]; double array2[N]; double array3[N]; for (i=0; i

Explanation / Answer

/*
The srand() function is used to set the starting point for producing a series of pseudo-random integers.

srand() must be called only once in a program and its effect is global , means if it is called in a function then its
effect will also we in other function.

Error: you can write the program outside the functions that will execute. Outside the functions you only can do declarations

Your srand() functions is part of execution so you can't write it outside the function.

so best practice is that srand() effect is global so declare it inside main() function


I wrote it inside the main method , please find the update code without errors.

*/


#include "stdio.h"

#include "time.h"

#include "stdlib.h"

#define N 10

void print_array2(double array[], int length);

//void bubble_sort(double array[], int length, in order);

//void insertion_sort(double array[], int length);

int i;


void main(){

//initializing srand function
srand(time(0));

double array1[N];

double array2[N];

double array3[N];

for(i=0; i<N;i++){

array1[i] = rand() % 200 - 100;

array2[i] = rand() % 200 - 100;

array3[i] = rand() % 200 - 100;

}

print_array2(array1,N);

print_array2(array2,N);

print_array2(array3,N);

/*

bubble_sort(array1,N,0);

bubble_sort(array2,N,1);

insertion_sort(array3,N);

print_array2(array1,N);

print_array2(array2,N);

print_array2(array3,N);

*/

}

void print_array2(double array[], int length){

for(i=0; i<N;i++){

printf("%f ", array[i]);

}

printf(" ");

}