according to the book "introduction to algorithms I tried sorting a matrix using column sort.here is my approach
1) sort each row of matrix -
#include<stdio.h>
#include<stdlib.h>
void sort(int arr[3][3], int k)// here k defines the column number ,k remains constant through out the function
//because we have to sort the matrix column wise respectively
{
int i;
int c[10]={0};
int b[3][3];
for(i=0;i<3;i++)
{ c[arr[i][k]]++;
}
for(i=1;i<3;i++)
{
c[i]+=c[i-1];
}
for(i=2;i>=0;i--)
{
b[c[arr[i][k]]-1][k]=arr[i][k];
c[arr[i][k]]--;
}
for(i=0;i<3;i++)
{
arr[i][k]=b[i][k];
}
}
I have passed k as an argument,which is the column number (as it remains constant throughout the sorting of each column, and only the row number changes,so i have iterated only over the row number )
the function to pass the desired column number which then calls the count function
void column(int arr[3][3]) // to call the function column wise by passing k as a parameter to count function
{
int k;
for(k=0;k<3;k++)
sort(arr,k);
}
2) transpose a matrix
{
int i,j,temp;
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
temp=arr[i][j];
arr[i][j]=arr[j][i];
arr[j][i]=temp;
}
}
the print function
void print(int arr[3][3]) // to print the output
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
}
the main function
{
int arr[3][3];
int i,j;
for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d ",&arr[i][j]);
}
// prints the array just inputed
print(arr);
// column calls the function sort according to the column
column(arr);
transpose(arr); // matrix is transposed
printf("\n");
print(arr); // matrix is printed
column(arr); // matrix is again passed with respect to the columns and sorted
transpose(arr); // matrix is transposed to get the original matrix
printf("\n");
print(arr); //final result is printed
return 0;
}
the output is very unlikely how can it be sorted correctly
Here is a working version of your code. I fixed the dropped bracket and the transpose function:
#include <stdio.h>
#include<stdlib.h>
void sort(int arr[3][3], int k)// here k defines the column number ,k remains constant through out the function
//because we have to sort the matrix column wise respectively
{
int i,j;
int tmp = 0;
for(i=0;i<2;i++)
{
for (j = 0; j < 3-i-1; j++)
{
if (arr[j][k] > arr[j+1][k])
{
tmp = arr[j][k];
arr[j][k] = arr[j+1][k];
arr[j+1][k] = tmp;
}
}
}
printf("\n");
}
void column(int arr[3][3]) // to call the function column wise by passing k as a parameter to count function
{
int k;
for(k=0;k<3;k++)
{
sort(arr,k);
}
}
void transpose(int arr[3][3])
{
int i,j,temp;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i != j && i<j)
{
temp=arr[i][j];
arr[i][j]=arr[j][i];
arr[j][i]=temp;
}
}
}
}
void print(int arr[3][3]) // to print the output
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
}
int main()
{
int arr[3][3] = {0};
int i,j;
for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d ",&arr[i][j]);
}
printf("\n");
// prints the array just inputed
print(arr);
printf("\n");
// column calls the function sort according to the column
column(arr);
print(arr);
transpose(arr); // matrix is transposed
printf("\n");
print(arr); // matrix is printed
column(arr); // matrix is again passed with respect to the columns and sorted
printf("\n");
print(arr); // matrix is printed
transpose(arr); // matrix is transposed to get the original matrix
printf("\n");
print(arr); //final result is printed
return 0;
}
Here is the output of: Sort, Transpose, Sort, then transpose:
Related
This is a piece of code to add the same number multiple times to an empty array but when I am printing the now non empty array, I am getting some other values:
#include<stdio.h>
#include<stdlib.h>
void sort_0(int arr[100], int i, int n){
int final_array[100], c=0;
// Count the number of '0' in the array
for(i=0;i<n;i++){
if(arr[i] == 0){
c++;
}
}
// Add the c number of '0' to the final_array
for(i=0;i<c;i++){
scanf("%d",final_array[i]);
}
for(i=0;i<c;i++){
printf("%d ", final_array[i]);
}
}
int main(){
int arr[100], i, n;
// Entering the size of the array
scanf("%d", &n);
// Entering n elements into the array
for(i=0;i<n;i++){
scanf("%d", &arr[i]);
}
sort_0(arr,i,n);
return 0;
}
In the above code, the number of times 0 appears in the array is counted. Then the count is taken as the range and 0 is adding to the empty array final_array count times.
If c = 5, the final_array = {0,0,0,0,0}
Expected Output:
arr = {0,1,4,3,0}
Output = 2
I am not getting any output
Since you don't know how much 0 you'll need to add to your array_final I figured out that a better solution could be to create that array after you have the number of 0 of the first array. Also, I see no reason why you were passsing i to the function since you can simply define it in the function itself.
void sort_0(int arr[10], int n, int* c){
int i;
for(i=0;i<n;i++){
if(arr[i] == 0){
(*c)+= 1;
}
}
}
int main (void) {
int size;
printf("Enter array size: ");
scanf("%d", &size);
int arr[size];
for (int i=0;i<size;i++) {
scanf("%d",&arr[i]);
}
int c = 0;
sort_0(arr, size, &c);
printf("C is: %d\n",c);
int* final_array;
if ((final_array=malloc(c * sizeof(int)))==NULL) // should always check malloc errors
{
perror("malloc");
return -1;
}
for (int i=0;i<c;i++) {
final_array[i]= 0;
}
printf("{");
for (int i=0;i<c-1;i++) {
printf("%d,", final_array[i]);
}
printf("%d}\n",final_array[c-1]);
return 0;
}
I have two functions. One that creates a multiplication table of a given number and the other function prints the array out. Below is my code:
Here's the error (Line 18):
expression must be a pointer to a complete object type
How do I fix this error and print the array? Also, I don't know how to print a new line after every row.
#include "multiplication.h"
#include <stdio.h>
int arr[][];
void mulitpication(int num){
/* initialize array and build*/
int arr[num][num];
for(int i=0; i<num;i++){
for(int j=0;j<num;j++){
arr[i][j]= (i+1)*(j+1);
}
}
}
void print(int arr[][]){
/* print the arr*/
int i;
for(i=0;i<sizeof(arr);i++){
for(int j=0;j<sizeof(arr);j++){
printf("%d ",arr[i][j])**(line 18)**;
}
}
}
If using C99 or later with VLA support, pass into the print function the dimensions needed.
// void print(int arr[][]){
void print(size_t rows, size_t cols, int arr[row][col]){
size_t r,c;
for (size_t r = 0; r < rows; r++) {
for (size_t c = 0; c < cols; c++) {
printf("%d ",arr[r][c]);
}
printf("\n");
}
}
You need to declare the array in main(), so it can be passed to both functions.
When an array is passed as a function parameter, it just passes a pointer. You need to pass the array dimensions, they can't be determined using sizeof.
To get each row of the table on a new line, put printf("\n"); after the loop that prints a row.
#include <stdio.h>
void multiplication(int num, arr[num][num]){
/* initialize array and build*/
for(int i=0; i<num;i++){
for(int j=0;j<num;j++){
arr[i][j]= (i+1)*(j+1);
}
}
}
void print(int num, int arr[num][num]){
/* print the arr*/
int i;
for(i=0;i<num;i++){
for(int j=0;j<num;j++){
printf("%d ",arr[i][j]);
}
printf("\n");
}
}
int main(void) {
int size;
printf("How big is the multiplication table? ");
scanf("%d", &size);
int arr[size][size];
multiplication(size, arr);
print(size, arr);
return 0;
}
I have to write a program in C which finds the sum and the product of two matrices.
I wrote the functions but I get stuck at calling them in main. I don't know which variable is for rows and columns of result matrix.
#include <stdio.h>
void enterMatrix(int a[][10], int rows, int columns)
{
int i,j;
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
printf("a(%d,%d)=",i,j);
scanf("%d",&a[i][j]);
}
}
}
void displayMatrix(int a[][10], int rows, int columns)
{
int i,j;
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
printf("%d", a[i][j]);
printf(" ");
}
printf("\n");
}
}
void matrixSum(int a[][10], int b[][10], int c[][10], int rows, int columns)
{
int i,j;
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
}
void matrixProduct(int a[][10], int b[][10], int c[][10], int rows, int columns)
{
int i,j,k;
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
c[i][j]=0;
for(k=0;k<columns;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
}
int main(void)
{
int a[10][10], b[10][10], sum[10][10], product[10][10];
int rowsA,columnsA,rowsB,columnsB;
printf("Number of rows for matrix A: \n");
scanf("%d",&rowsA);
printf("Number of columns for matrix A: \n");
scanf("%d",&columnsA);
printf("Number of rows for matrix B: \n");
scanf("%d",&rowsB);
printf("Number of columns for matrix B: \n");
scanf("%d",&columnsB);
printf("Enter first matrix: \n");
enterMatrix(a,rowsA,columnsA);
printf("Show first matrix: \n");
displayMatrix(a,rowsA,columnsA);
printf("Enter second matrix: \n");
enterMatrix(b,rowsB,columnsB);
printf("Show second matrix: \n");
displayMatrix(b,rowsB,columnsB);
if((rowsA==rowsB) && (columnsA==columnsB))
{
matrixSum(a,b,sum, ???, ???);
printf("The sum matrix is: \n");
displayMatrix(sum, ???, ???);
}
else
{
printf("Wrong information.");
}
if((rowsA==columnsB) && (rowsB==columnsA))
{
matrixProduct(a,b,product,???,???);
printf("The product matrix is \n");
displayMatrix(product,???,???);
}
return 0;
}
For matrixSum you just give rowsA and columnsA, as they are equal to rowsB and columnsB.
For matrixProduct you need three numbers: rowsA, columnsA and columnsB. rowsB is not needed, as it is equal to columnsA.
You need to change your matrixProduct function to use these three numbers at the correct places. Your current code doesn't work except when all numbers are equal.
Also your test if((rowsA==columnsB) && (rowsB==columnsA)) before calling matrixProduct only needs if(rowsB==columnsA).
if((rowsA==rowsB) && (columnsA==columnsB))
{
matrixSum(a,b,sum, rowsA, columnsA);
printf("The sum matrix is: \n");
displayMatrix(sum, rowsA, columnsA); // the sum has the same dimensions as A and B
}
else
{
printf("Both matrices don't have equal dimension.\n");
}
if(rowsB==columnsA)
{
matrixProduct(a,b,product,rowsA,columnsA,columnsB);
printf("The product matrix is \n");
displayMatrix(product,rowsA,columnsB); // the product matrix has the
// number of rows of A and the number of columns of B
}
else
{
printf("The number of columns of A needs to be equal to the number or rows of B.\n");
}
Your matrixProduct function could be adapted as follows:
void matrixProduct(int a[][10], int b[][10], int c[][10], int rowsA, int columnsA, int columnsB)
{
int i,j,k;
for(i=0;i<rowsA;i++)
{
for(j=0;j<columnsB;j++)
{
c[i][j]=0;
for(k=0;k<columnsA;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
}
I'm supposed to multiply a certain line (I specify what line exactly in the 4th argument of the function) of a given matrix by a number.
The main function:
int main_tp05(int argc, const char *argv[]){
int mNx100[][MAXCOLS100] = {{1,2,3},{4,5,6},{7,8,9}};
multiply_matrixNx100_line_by_scalar(mNx100,3,3,1,2);
return 0;
}
I've tried to solve it like so:
void multiply_matrixNx100_line_by_scalar(int mNx100[][MAXCOLS100], int lines, int columns, int line, int scalar){
for (int i = 0; i < lines; i++) {
for (int j = 0; j < columns; j++) {
if(i == line){
printf("%d\n", mNx100[i*scalar][j] );
}
}
printf("\n");
}
}
To note that:
1- I canĀ“t change the parameters.
2- MAXCOLS100 is a macro on the .h file. I put it with the value of 3.
3- The scalar is the number I want to multiply the line by.
I WROTE THIS CODING AND VERIFIED BY MYSELF IT WORKS SUCCESSFULLY.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,a[5][5],b,c,d,m,n,r;
printf("Enter the number of rows and columns\n");
scanf("%d %d",&m,&n);
printf("Enter the matrix\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the number to multiply the matrix\n");
scanf("%d",&b);
printf("Enter 0 to multiply row or 1 to multiply column\n");
scanf("%d",&d);
if(d==0)
{
printf("Enter the row number to be multiplied\n");
scanf("%d",&r);
for(i=r,j=1;j<=n;j++)
{
a[i][j]*=b;
}
}
else if(d==1)
{
printf("Enter the row number to be multiplied\n");
scanf("%d",&c);
for(j=c,i=1;i<=m;i++)
{
a[i][j]*=b;
}
}
printf("matrix after multiplied\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT OF THIS CODING
I'm supposed to multiply a certain line (I specify what line exactly in the 4th argument of the function) of a given matrix by a number
Usually, "multiply a row of a matrix by a scalar" means to multiply every element of the row by a certain scalar value. That's not what the posted function does, it multiplies the index of the row by the passed argument:
void multiply_matrixNx100_line_by_scalar(int mNx100[][MAXCOLS100], int lines, int columns,
int line, int scalar) {
for (int i = 0; i < lines; i++) { // <-- Useless, the row is known
for (int j = 0; j < columns; j++) {
if(i == line){
printf("%d\n", mNx100[i*scalar][j] );
// ^^^^^^^^
}
}
printf("\n");
}
}
If the intent is to only print the modified row, the previous function could be rewritten as
void multiply_matrixNx100_line_by_scalar(int mNx100[][MAXCOLS100], int lines, int columns,
int line, int scalar) {
if (line < 0 || line >= lines)
return;
for (int j = 0; j < columns; j++) {
printf("%d\n", mNx100[line][j] * scalar);
}
printf("\n");
}
If instead the function is supposed to only modify the matrix, without printing it, we could use
mNx100[line][j] *= scalar;
Inside the loop, in place of the call to printf.
Write a program which should get 5 floating values from the user in an array using a function insert and then sort these values using a function sort after that print the sorted values on the screen using a function disp.
This is my code please tell me where i have done something wrong.It prints 0.000 after sorting.
#include <stdio.h>
void insert(float array[],int val);
void disp(float array[],int val);
void sort(float array[], int val);
void main ()
{
float array[5],j;
printf("Enter numbers: \n");
insert(array,5); //array input function
printf("Enter numbers are: \n");
disp(array,5); //array output function
sort(array,5); //array sort function
printf("\nSorted Array is: \n");
disp(array,5); //array output function
}
//array input function
void insert(float array[],int val)
{
int k,i;
for (k = 0;k<5;k++){
scanf("%f",&array[k]);
}
}
//array sort function
void sort(float array[], int val){
int i,j;
float hold;
for(i=0; i<6; i++)
{
for(j=0; j<6; j++)
{
if(array[j]>array[j+1])
{
hold=array[j];
array[j]=array[j+1];
array[j+1]=hold;
}
}
}
}
//array display function
void disp(float array[],int val)
{
int k;
for (k = 0;k<5;k++){
printf("%f\n",array[k]);
}
}
Sort function should look like this:
//array sort function
void sort(float array[], int val)
{
int i,j;
float hold;
for(i=0; i<4; i++)
{
for(j=0; j<4; j++)
{
if(array[j]>array[j+1])
{
hold=array[j];
array[j]=array[j+1];
array[j+1]=hold;
}
}
}
}
In the original code first loop executes two times too many.
Second loop causes reading beyond the array. Since you have 5 elements you need 4 comparisons.
First of all, you declared some variables that you didn't use, especially int val in your functions.
Secondly, you don't need to implement a function to insert values in an array, just loop it with a scanf directly.