Question

In: Computer Science

#include<stdio.h> #include<stdlib.h> int main() {     //Q1) Note: You can use only pointer based operations while...

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

int main()
{
    //Q1) Note: You can use only pointer based operations while implementing each of the functionalities below
   // zero points will be given if pointer operations are not used in implementing
   // each of the operations below.
   // Also, use C code only in visual studio or GCC in general.asu.edu server


   /* i.) Create a 2 - D array of integers using pointers (use dynamic memory allocation).
           Assume that the array has 4 rows and 5 columns.Then,
           fill the array with randomly generated int values between 1 and 100. */

   /* ii.) Find the largest values in the array */

   /*
   iii.) Find the smallest value in the 1st row
  
   */


   /*
   iv.) Find the smallest value in the 1st column
   */

   /*v.) Display the elements in the diagonal
   */


   return 0;

C language and please comment on your code

Solutions

Expert Solution

Code

#include <stdio.h>

#include<stdlib.h>

int main(void) {

int r = 4, c = 5;

int i,j,num;

//dynamiclly assing memeory to 2d array

int *arr = (int *)malloc(r * c * sizeof(int));

int min,max;

//add value to the array

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

for (j = 0; j < c; j++)

{

*(arr + i*c + j) = rand()%(100-1) + 1;

}

max=*(arr + 0*c + 0);

//print array

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

{

for (j = 0; j < c; j++)

{

printf("%d\t",*(arr + i*c + j));

if(*(arr + i*c + j)>min)

max=*(arr + i*c + j);

}

printf("\n");

}

printf("\nThe largest value in the 1st row: %d\n",max);

//finding min from 1st row

min=*(arr + 0*c + 0);

i=0;

for (j = 0; j < c; j++)

{

if(*(arr + i*c + j)<min)

min=*(arr + i*c + j);

}

printf("\nThe smallest value in the 1st row: %d\n",min);

//finding min from 1st col

min=*(arr + 0*c + 0);

i=0;

for (j = 0; j < r; j++)

{

if(*(arr + i + j*c)<min)

min=*(arr + i + j*c);

}

printf("\nThe smallest value in the 1st col: %d",min);

//prints diagonal elements

printf("\n\nDiagonal Elemnts: ");

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

for (j = 0; j < c; j++)

if(i==j)

printf("%d\t",*(arr + i*c + j));

return 0;

}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

Note: You can use only pointer based operations while implementing each of the functionalities below// zero...
Note: You can use only pointer based operations while implementing each of the functionalities below// zero points will be given if pointer operations are not used in implementing // each of the operations below. // Also, use C code only in visual studio or GCC i) Create a 2 - D array of integers using pointers (use dynamic memory allocation) in C. Assume that the array has 4 rows and 5 columns.Then, fill the array with randomly generated int values...
#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)       ...
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> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;     char fname[20];     //int sum = 0;     int i, j, k, tmp =0;     int num = 0;     int mass = 0;     int count = 0;     int fuel = 0;     int total = 0;     int M[1000];     char ch;     char buffer[32];     printf(" Input the filename to be opened : ");     scanf("%s",fname);     myFile = fopen(fname, "r");     if(myFile == NULL)     {         printf("Can't open file\n");     } while(1)     {         ch =...
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...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main(...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main( int argc, char* argv[] ) { // Size of vectors int n = 10000; // Input vectors double *restrict a; double *restrict b; // Output vector double *restrict c; // Size, in bytes, of each vector size_t bytes = n*sizeof(double); /* Q1: Allocate memory for vector a (10 points)*/ /* Q2: Allocate memory for vector b (10 points)*/ /* Q3: Allocate memory for vector...
#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++;    }...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter between 8 to 60 inches: "); scanf("%d",&diameter); // if(diameter>60 || diameter<=8){ // printf("Error! invalid input"); // exit(0); // } // else{ // float radius = diameter/2; // float volume = (4/3)*PI*radius*radius*radius; // printf("%.2f",volume); // } //check through the while loop if it is valid or in valid while(diameter>60 || diameter<=8){ printf("Invalid input Enter again: "); scanf("%d",&diameter); }    //caluclate the volume of sphere float...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT