PDA

View Full Version : مبتدی: برنامه نویسی به زبان c



رضا 920
پنج شنبه 18 دی 1393, 18:27 عصر
سوال :

یک کامپیوتر ارایه دو بعدی سه در چهار را از ورودی دریافت کرده جای ستون دوم و چهارم را باهم عوض کنید
include#<stdio.h>
include#<conio.h>

mehran34
پنج شنبه 18 دی 1393, 20:55 عصر
#include "stdio.h"
#include "conio.h"

#define ROW_COUNT 5
#define COLUMN_COUNT 5



int swap (int *a , int *b){
(*a)^=(*b);
(*b)^=(*a);
(*a)^=(*b);
return 0;

}

int main(){
int i, j ;
int swp1, swp2 ;
int a[ROW_COUNT][COLUMN_COUNT];

// Create ( ROW_COUNT X COLUMN_COUNT ) random matrix

for (i = 0 ; i < ROW_COUNT ; i++ )
for (j = 0 ; j < COLUMN_COUNT ; j++ ) a[i][j] = rand();

// Display matrix before row swap

for (i = 0 ; i < ROW_COUNT ; i++ ){
for (j = 0 ; j < COLUMN_COUNT ; j++ ) printf("%d\t",a[i][j]);
printf("\n");
}

// Elements to be swapped

printf("\nSwap Row: "); scanf("%d", &swp1) ; // first row index
printf("With Row: "); scanf("%d", &swp2); // second row index

// Swapping right here

for (j = 0 ; j < COLUMN_COUNT ; j++){
swap( &a[swp1][j] , &a[swp2][j] );
}


// Display once again

printf("\n");
for (i = 0 ; i < ROW_COUNT ; i++ ){
for (j = 0 ; j < COLUMN_COUNT ; j++ ) printf("%d\t",a[i][j]);
printf("\n");
}



getch();
return 0;
}


سلام این واسه دوتا سطر هست واسه ستونشم سعی کنید خودتون با توجه به این کد بنویسید :قلب: ولی خودمم سعی میکنم بنویسمش و بگذارمش موفق باشید

mehran34
پنج شنبه 18 دی 1393, 21:18 عصر
اینم کد کامل هم برای سطر هم برای ستون :قلب:

/*

* C program to accept a matrix of given order and interchange

* any two rows and columns in the original matrix

*/

#include <stdio.h>



void main()

{

static int array1[10][10], array2[10][10];

int i, j, m, n, a, b, c, p, q, r;



printf("Enter the order of the matrix \n");

scanf("%d %d", &m, &n);

printf("Enter the co-efficents of the matrix \n");

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

{

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

{

scanf("%d,", &array1[i][j]);

array2[i][j] = array1[i][j];

}

}

printf("Enter the numbers of two rows to be exchanged \n");

scanf("%d %d", &a, &b);

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

{

/* first row has index is 0 */

c = array1[a - 1][i];

array1[a - 1][i] = array1[b - 1][i];

array1[b - 1][i] = c;

}

printf("Enter the numbers of two columns to be exchanged \n");

scanf("%d %d", &p, &q);

printf("The given matrix is \n");

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

{

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

printf(" %d", array2[i][j]);

printf("\n");

}

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

{

/* first column index is 0 */

r = array2[i][p - 1];

array2[i][p - 1] = array2[i][q - 1];

array2[i][q - 1] = r;

}

printf("The matix after interchanging the two rows(in the original matrix) \n");

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

{

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

{

printf(" %d", array1[i][j]);

}

printf("\n");

}

printf("The matix after interchanging the two columns(in the original matrix) \n");

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

{

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

printf(" %d", array2[i][j]);

printf("\n");

}

}

رضا 920
جمعه 19 دی 1393, 11:22 صبح
تشکر ویژه و مرسی از شما .:قلب::تشویق: