So, my functions in my code don't talk to each other and I cannot for the life of me figure it out. What do you put in the parenthesis of the function headings. the variable names go in them but how are they formatted? Do I need pointers? If so can someone explain them. Thanks!
int FindTranspose(????)
int main(????)
FindTranspose (????)
#include <stdio.h>
int FindTranspose(int before[3][3], int after[3][3], int i, int j){
int rows, columns;
printf("\nThe orginial matrix: \n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++){
printf("%d ", before[i][j]);
}
}
for(rows = 0; rows < i; rows++){
for(columns = 0;columns < j; columns++){
after[columns][rows] = before[rows][columns];
}
}
printf("\n");
printf("\nThe Transpose of the matrix: \n");
printf("\n");
for(rows = 0; rows < j; rows++){
for(columns = 0; columns < i; columns++){
printf("%d ", after[rows][columns]);
}
printf("\n");
}
}
int main(){
int before[3][3], after[3][3], i, j;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("Enter number for array - [%d],[%d] : ",i,j);
scanf("%d", &before[i][j]);
}
}
FindTranspose (before[3][3], after[3][3], i, j);
return 0;
}
Picture of code
Picture of warnings
Dealing with multi-dimensional arrays as pointers is really tricky in C.
For example the multi-dimensional array before[3][3] defined in main:
The array name before is not "as usual" a pointer to before[0][0] although it's a pointer to before[0] so before actually has the type int (*) [3] which read as 'Pointer to a one-dimensional array of 3 elements'
Anyway, you should learn more about "pointer and multi-dimensional arrays".
For the moment here is the solution to your issue:
#include <stdio.h>
void FindTranspose(int (*before)[3], int (*after)[3], int i, int j){
int rows, columns;
printf("\nThe orginial matrix: \n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++){
printf("%d ", before[i][j]);
}
}
for(rows = 0; rows < i; rows++){
for(columns = 0;columns < j; columns++){
after[columns][rows] = before[rows][columns];
}
}
printf("\n");
printf("\nThe Transpose of the matrix: \n");
printf("\n");
for(rows = 0; rows < j; rows++){
for(columns = 0; columns < i; columns++){
printf("%d ", after[rows][columns]);
}
printf("\n");
}
}
int main(){
int before[3][3], after[3][3], i, j;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("Enter number for array - [%d],[%d] : ",i,j);
scanf("%d", &before[i][j]);
}
}
FindTranspose (before, after, i, j);
return 0;
}
Of course, there is an easier but complex to understand solution which is dealing with the multi-dimensional array as a one-dimensional array.
Here is an example of that:
#include <stdio.h>
#define NUM_OF_ROWS 3
#define NUM_OF_COLS 3
void FindTranspose(int *before){
int rows, columns, i;
int after[NUM_OF_ROWS][NUM_OF_COLS];
int *p, (*q)[NUM_OF_COLS];
printf("\nThe orginial matrix: \n");
i = 0;
for(p = before; p < before + NUM_OF_COLS * NUM_OF_ROWS; p++){
printf("%d ", *p);
if(++i >= NUM_OF_COLS){
i = 0;
printf("\n");
}
}
columns = 0;
for(p = before; p < before + NUM_OF_COLS * NUM_OF_ROWS;){
for(q = &after[0]; q < &after[NUM_OF_ROWS]; q++){
(*q)[columns] = *p++;
}
columns++;
}
printf("\n");
printf("\nThe Transpose of the matrix: \n");
for(rows = 0; rows < NUM_OF_ROWS; rows++){
for(columns = 0; columns < NUM_OF_COLS; columns++){
printf("%d ", after[rows][columns]);
}
printf("\n");
}
}
int main(){
int before[NUM_OF_ROWS][NUM_OF_COLS], i, j;
for(i=0; i<NUM_OF_ROWS; i++){
for(j=0; j<NUM_OF_COLS; j++){
printf("Enter number for array - [%d][%d]: ", i, j);
scanf("%d", &before[i][j]);
}
}
FindTranspose ( &before[0][0] );
return 0;
}
Recommended reading: C Programming A modern approach 2nd edition for K.N. King
Related
How can I copy the elements from a matrix,entered by user,to an array? I tried this, but it didn't work:
#include<stdio.h>
int main(){
int m[20][20],a[400],c=0;//max dimensions;
scanf("%d %d",&M,&N);//dimensions of matrix;
for(i=0;i<M;i++{
for(j=0;j<N;j++{
scanf("%d", &m[i][j]);
for(c=0;c<M*N;c++)
a[c]=m[i][j];
}}}
Don't know why you want to store both the matrix format and the array format, but anyway here is a code that should do the trick with also the data output to show the result:
#include <stdio.h>
#include <stdlib.h>
#define R 20 //max rows
#define C 20 //max columns
int main() {
int m[R][C]; //matrix
int a[R*C]; //array
int r, c; //user matrix size
int i, j; //iterators
printf("insert row size: ");
scanf("%d", &r);
printf("insert column size: ");
scanf("%d", &c);
if(r > R || c > C) {
printf("Invalid sizes");
return -1;
}
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++) {
printf("insert value for row %d column %d: ", i + 1, j + 1);
scanf("%d", &m[i][j]);
a[(c * i) + j] = m[i][j];
}
}
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++) {
printf("%d ", m[i][j]);
}
printf("\n");
}
for(i = 0; i < r * c; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
Note that I also added some checks for the user data, avoiding to generate a matrix bigger that the maximum size. Also you don't need separate loops but it can be done all together.
Also please post a code that is compilable and can be run, as explained here: https://stackoverflow.com/help/how-to-ask
I need to make some functions for 2-dimensional arrays (a.k.a. matrixes) and the problem just starts with the first one.
I allocated the matrix in the heap memory using malloc() and then I tried to make a function that initializes the matrixes using a for loop of scanf()s, but after 3 times it takes the input, the program crashes.
Here is the code I made
void initMatrix(int **mat,int row,int col)
{
for(int i=0;i<row;i++){
printf("\n");
for(int j=0;j<col;i++){
printf("Cell (%d,%d): ",i,j);
scanf("%d",&mat[i][j]);
}
}
}
int main()
{
int r,c;
printf("Number of rows: ");
scanf("%d",&r);
printf("Number of columns: ");
scanf("%d",&c);
int **arr = (int **)malloc(sizeof(int*) * r);
for(int i=0;i<r;i++)
arr[i]=(int*)malloc(sizeof(int) * c);
// int count=0;
// for (int i = 0; i < r; i++)
// for (int j = 0; j < c; j++)
// arr[i][j] = ++count;
for (int i = 0; i < r; i++){
printf("\n");
for (int j = 0; j < c; j++)
printf("%d ", arr[i][j]);
}
initMatrix(arr,r,c);
printf("\n");
free(arr);
}
If I manually insert the content of the matrix, the program does work (without the initmatrix() function), I don't know why… I probably made a mistake somewhere.
How to return array of elements from a void function in another c file. My assignment is to reverse the array in lab8.c and then use main.c to show the results I have no idea how to reverse the elements or print the results in main. Functions outside of my main cannot use printf or scanf
main.c
#include <stdio.h>
#include "lab8.h"
int main(void) {
int x[100];
int y[100];
int n = 0;
int count, i, product;
printf("Enter the length of both arrays\n");
scanf("%d", &count);
printf("Enter the %i elements of the first array\n", count);
for(i=0; i<count; i++){
scanf("%i", &x[i]);
}
printf("Enter the %i elements of the second array\n", count);
for(i=0; i<count; i++){
scanf("%i", &y[i]);
}
product = inner_product(x, y, count);
printf("Inner product of first array and second: %i\n", product);
printf("Enter the %i elements of the array\n", count);
for(i=0; i<count; i++){
scanf("%i", &n[i]);
}
reverse(n, count);
printf("Reverse of array 1: %i\n", n);
return(0);
}
lab8.c
#include <stdio.h>
#include "lab8.h"
int inner_product(int a[], int b[], int count){
int i;
int result = 0;
for( i=0; i<count; i++){
result = result + (a[i] * b[i]);
}
return result;
}
void reverse(int a[], int count){
int i, r, end = count - 1;
for(i=0; i<count/2; i++)
r = a[i];
a[i] = a[end];
a[end] = r;
end--;
}
There is one mistake in your code, the for loop in the reverse() function has no braces and hence it's just executing r = a[i] count / 2 times.
void reverse(int a[], int count) {
int i, r, end = count - 1;
for (i = 0 ; i < count / 2 ; i++) {
r = a[i];
a[i] = a[end];
a[end] = r;
end--;
}
}
and to print the result in main() just
reverse(x, count);
printf("Reverse of array 1: %i\n", n);
for (i = 0 ; i < count ; ++i)
printf("%d ", x[i]);
printf("\n");
also, do not ignore the return value of scanf() if it doesn't succeed at reading the values your program will invoke UNDEFINED BEHAVIOR, you should learn good practices right from the beginning.
I want to delete duplicates values in array. For example: array[1,5,6,1,3,5,9] I want to have array[6,3,9].
I have written this, but I am having troubles:
#include<stdio.h>
main() {
int array[50], i, j, k=0, c=0, array2[50], n;
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("array[%d]= ", i); scanf("%d", &array[i]);
}
for (i = 0; i < n; ) {
for (j = i + 1; j < n; j++) {
if (array[i] == array[j])
i++;
else {
array2[k++] = array[i];
c++;
}
}
}
for (k = 0; k < c; k++) {
printf("%d ", array2[k]);
}
system("pause");
}
You should start by describing your problem in pseudo code, and breaking it into smaller pieces.
Start from scratch by deleting all these redundant variables, and try to implement the following algorithm:
for each element in inputArray
if not elementIsDuplicate(element)
add to outputArray
That's a single for loop. The elementIsDuplicate is separate function.
Now try to implement the function elementIsDuplicate. This function also contains a single loop, takes input parameters (int* array, int n, int currentIdx) and returns 1 or 0 indicating whether the element at currentIdx occurs anywhere else in the array.
#include<stdio.h>
int main(){
int array[50], i, j, k=0, c, n, array2[50] = {0};
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; ++i){
int num, dup = 0;
printf("array[%d]= ", i); scanf("%d", &num);
for(j = 0; j < k; ++j){
if(array[j] == num){
array2[j] = dup = 1;
break;
}
}
if(!dup){
array[k++] = num;
}
}
for (c=i=0; i < k; ++i){
if(!array2[i])
printf("%d ", array[c++] = array[i]);
}
printf("\n");
/*
for(i=0;i<c;++i)
printf("%d ", array[i]);
printf("\n");
*/
system("pause");
return 0;
}
#include<stdio.h>
main()
{
int n, a[50], b[50], count = 0, c, d;
printf("Enter number of elements in array\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for(c=0;c<n;c++)
scanf("%d",&a[c]); //enter array elements
for(c=0;c<n;c++)
{
for(d=0;d<count;d++)
{
if(a[c]==b[d])
break;
}
if(d==count)
{
b[count] = a[c];
count++;
}
}
printf("count is: %d\n",count);
printf("Array obtained after removing duplicate elements\n");
for(c=0;c<count;c++)
printf("%d\n",b[c]);
return 0;
}
This will remove multiple duplicates from the desired array..
Example: if the input array is: 3 6 5 6 2 8 6 5 9 8 6 ,,then the output array will be: 3 6 5 2 8 9
I'm trying to print a 2d array with a function, but I keep getting the error "pointer expected"
I'm trying to make a battleship-type grid. I'm fine with printing out the co-ordinate row and column, but I can't actually get the 2d array (which contains "." in every element) to print at all.
Any help would be appreciated, I'm very new to this. Thanks! :)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int length;
int width;
int i;
int j;
char invisible_board;
void board_setup(int *rows, int *columns){
char *invisible_board[*rows][*columns];
char *player_board[*rows][*columns];
for (i = 0; i < *rows; i++){
for (j = 0; j < *columns; j++){
invisible_board[i][j] = "."; //Sets all elements in hidden board to water
}
}
for (i = 0; i < *rows; i++){
for (j = 0; j < *columns; j++){
player_board[i][j] = ".";
}
}
}
void display(int *rows, int *columns, char *invisible_board){
printf(" ");
for (i=1; i < *rows +1;i++){
printf("%d ",i);
}
printf("\n"); //Prints top row of co-ordinates
for (i=1; i < *columns+1;i++){
printf("%d ",i);
for (j=0;j < *columns;j++){ //Prints left column of co- ordinates and rows of game board
printf(" %c ",invisible_board[i-1][j]);
}
printf("\n");
}
}
int main(void){
printf("Please enter the amount of rows in your board\n");
scanf("%d",&length);
printf("Please enter the amount of columns in your board\n");
scanf("%d",&width);
board_setup(&length,&width);
display(&length,&width,&invisible_board);
return (0);
}
this is the simplest changes I could make to your code to get you to working code.... now.... this isn't good code yet. But gets you started.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int length;
int width;
int i;
int j;
char invisible_board[100][100]; // dynamically allocate....
char player_board[100][100]; // dynamically allocate....
void board_setup(int *rows, int *columns){
for (i = 0; i < *rows; i++){
for (j = 0; j < *columns; j++){
invisible_board[i][j] = '.'; //Sets all elements in hidden board to water
}
}
for (i = 0; i < *rows; i++){
for (j = 0; j < *columns; j++){
player_board[i][j] = '.';
}
}
}
void display(int *rows, int *columns){
printf(" ");
for (i=1; i < *rows +1;i++){
printf("%d ",i);
}
printf("\n"); //Prints top row of co-ordinates
for (i=1; i < *columns+1;i++){
printf("%d ",i);
for (j=0;j < *columns;j++){ //Prints left column of co- ordinates and rows of game board
printf(" %c ",invisible_board[i-1][j]);
}
printf("\n");
}
}
int main(void){
printf("Please enter the amount of rows in your board\n");
scanf("%d",&length);
printf("Please enter the amount of columns in your board\n");
scanf("%d",&width);
board_setup(&length,&width);
display(&length,&width);
return (0);
}