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

1. In function main, prompt the user for a time in seconds. Call a function to c

ID: 3718419 • Letter: 1

Question

1. In function main, prompt the user for a time in seconds. Call a function to calculate the equivalent time in hours, minutes, and seconds. Parameters should be the time in total seconds and pointers to the hours, minutes, and seconds. Print the equivalent in hours, minutes, and seconds in function main. (For example, 36884 seconds is equal to 10 hours, 14 minutes, and 44 seconds.)

2. In function main, declare a two-dimensional integer array with 5 rows and 6 columns and other variables for the minimum, row and column number, number of values evenly divisible by five and sum of these numbers, and any other necessary variables. Call the following functions from function main.

Call a function createArray to seed the random number generator and to fill the two-dimensional array with random integers between 1 and 250. Parameters for this function should be the array and the number of rows and columns or just the array if you are using symbolic constants for the number of rows and columns.

Call a second function minimum to find the smallest value in the array and its location. Return the smallest value and its row and column number as pointer parameters. (The function will have six parameters: the array, number of rows and columns and pointers to the largest value and its row and column number.

If you are using symbolic constants for the number of rows and columns there will be only four parameters.)

Call a function fives to calculate the number of values evenly divisible by 5 and their sum. Return these values to function main using pass by address with pointer parameters. The function should have five parameters: the array, the number of rows and columns, and pointers to the number of values and their sum. If you are using symbolic constants for the number of rows and columns there will be only three parameters.

Call a function printResults to print the array, smallest value and its row and column number, and the number of values evenly divisible by 5 and their sum. There will be six parameters, not counting numbers of rows and columns, none of which needs to be a pointer.

Function main should have only declarations and call statements.

For 2 points extra credit:

In function main declare three integers a, b, and c. Call a function randomNumbers to assign a random number between 1 and 50 to each and return using pointer parameters. Print the integers and their addresses in a function printNumbers.

Then call the function order to place the integers in numerical order using pass by address. Function order should have three parameters: int *aPtr, int *bPtr, and int *cPtr. The purpose of function order is to place the three integers in order so that a<b<c.    (It is not necessary to implement a complicated sorting algorithm. The values can be placed in order using three comparisons.)

The integers should be printed in order with addresses from printNumbers. When the steps of the functionorder have been completed the smallest value will be stored in a, the middle in b, and the largest in c.

Please use stdio.h, no iostream!!!

Thanks!!!

Explanation / Answer

Answer :

1)

#include<stdio.h>

void convert(int n){
  
int hours,minutes,seconds; // variable declaration
printf("%d can be broken into ",n);
if(n>3600) // n>3600
{
minutes = n/60; // calculate minutes
seconds = n%60; // calculate seconds
hours = minutes/60; // calculate hours
minutes = minutes%60;
printf("%d hour %d mins %d secs",hours,minutes,seconds);
}
else
{
minutes = n/60;
seconds = n%60;
printf("%d mins %d secs",minutes,seconds);
}
  
}
int main()
{
int n;
printf("Enter seconds :");
scanf("%d",&n); // Accept n value
convert(n);
}

2)

#include <stdio.h>
#include <stdlib.h>
void createArray(int arr[5][6], int row, int col){
int i,j,n;
time_t t;
/* Intializes random number generator */
srand((unsigned) time(&t));

for(i=0;i<row;i++){
for(j=0;j<col;j++){
n=(rand() % 250)+1; //The random number generated between 1 to 250
arr[i][j]=n;
}
}
}
int minimum(int arr[5][6],int row,int col,int *minRow,int *minCol){
int i,j,smallest;
smallest=arr[0][0]; //initialise smallest to be the first element.
for(i=0;i<row;i++){
for(j=0;j<col;j++){
if(arr[i][j]<smallest){
smallest=arr[i][j];
*minRow=i;
*minCol=j;
}
}
}
return smallest;
}
void fives(int arr[5][6],int row,int col,int *divisible, int *sum){
int i,j,count=0,totalSum=0;
for(i=0;i<row;i++){
for(j=0;j<col;j++){
if(arr[i][j]%5==0){ //Check if the value is divisible by 5
count++;
totalSum+=arr[i][j];
}
}
}
*divisible=count;
*sum=totalSum;
}
void printResults(int arr[5][6],int row,int col,int *min,int *minRow,int *minCol,int *divisible,int *sum){
int i,j;
/*Prints the array*/
printf("Array: ");
for(i=0;i<row;i++){
for(j=0;j<col;j++){
printf("%3d ",arr[i][j]);
}
printf(" ");
}

/*Prints the rest of the values*/
printf("Smallest value: %d ",*min);
printf("Row: %d Column:%d ",*minRow+1,*minCol+1); //You can remove the +1 to view the array index itself

printf("Number of values evenly divisible by 5: %d ",*divisible);
printf("Sum: %d ",*sum);
}
/*Extra credit functions*/
void printNumbers(int *a,int *b,int *c){
/*We are using pointers here because we need to show the address of the original variable as asked in question*/
printf(" Value of a : %d Address of a : %d ",*a,a); //*a give value while a here returns address as a is a pointer in this function
printf(" Value of b : %d Address of b : %d ",*b,b); //*a give value while a here returns address as a is a pointer in this function
printf(" Value of c : %d Address of c : %d ",*c,c); //*a give value while a here returns address as a is a pointer in this function
}
void randomNumbers(int *a, int *b, int *c){
int n;
time_t t;
/* Intializes random number generator */
srand((unsigned) time(&t));
n=(rand() % 50)+1;
*a=n; //assign a random number to a

n=(rand() % 50)+1;
*b=n; //assign random number to b

n=(rand() % 50)+1;
*c=n; //assign random number to c

}
void order(int *aPtr,int *bPtr,int *cPtr){
if(*aPtr > *cPtr){
*aPtr=*aPtr+*cPtr; //swapping logic
*cPtr=*aPtr-*cPtr;
*aPtr=*aPtr-*cPtr;
}
if(*aPtr > *bPtr){
*aPtr=*aPtr+*bPtr; //swapping logic
*bPtr=*aPtr-*bPtr;
*aPtr=*aPtr-*bPtr;
}
/*Now the smallest element is in a. Next just compare b and c*/
if(*bPtr > *cPtr){
*bPtr=*bPtr+*cPtr; //swapping logic
*cPtr=*bPtr-*cPtr;
*bPtr=*bPtr-*cPtr;
}
}
void main(){
int arr[5][6],min,minRow,minCol,divisible,sum,a,b,c;
createArray(arr,5,6);
min=minimum(arr,5,6,&minRow,&minCol);
fives(arr,5,6,&divisible,&sum);
printResults(arr,5,6,&min,&minRow,&minCol,&divisible,&sum);
/*Extra credit questions*/
randomNumbers(&a,&b,&c);
printNumbers(&a,&b,&c);
order(&a,&b,&c);
printf(" After sorting "); //You can remove this line
printNumbers(&a,&b,&c);
}