In last lab, you were asked to put 15 numbers in an array A[15]. Everyone come t
ID: 3681114 • Letter: I
Question
In last lab, you were asked to put 15 numbers in an array A[15]. Everyone come to class did this.
(1) Write a program that test if an array A has the property of a sorted array. This means that this array contains the number that from small to large.
For instance, 1,4,6,7,9. is a sorted array. 2,3,-1,6,4 is not sorted. Print Yes or No for your findings. This problem is a little challenging.
(2) Write a program that can assign values to a 5x4 array meaning that A[5][4] should be filled from the keyboard input.
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
2.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int r = 5, c = 4, i, j, count;
int *arr[r];
for (i=0; i<r; i++)
arr[i] = (int *)malloc(c * sizeof(int));
// Note that arr[i][j] is same as *(*(arr+i)+j)
count = 0;
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
arr[i][j] = ++count; // Or *(*(arr+i)+j) = ++count
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
printf("%d ", arr[i][j]);
/* Code for further processing and free the
dynamically allocated memory */
return 0;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.