i have this code how to read the bidimensional array using a function?
i write this function it works read all the numbers but when i output to console the array there are not the values that i entered
ex
Input:
2 1 2 3 4
Output:
16 256
1 4525376
#include <stdio.h>
#include <stdlib.h>
void citMat(int a, int n) {
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
printf("a[%d][%d]",i,j);
scanf("%d", &a);
}
}
int main()
{ int i,j;
int a[10][10],n;
printf("Introdu n:");
scanf("%d", &n);
citMat(a[10][10],n);
for(i=1;i<=n;i++){
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
return 0;
}
You need to change the prototype to (Here array dimension is important)
void citMat(int a[10][10], int n)
Other changes are explained by others (The whole code is below)
#include <stdio.h>
#include <stdlib.h>
void citMat(int a[10][10], int n) {
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
printf("a[%d][%d]:",i,j);
fflush(stdout);
scanf("%d", &a[i][j]);
}
}
int main()
{ int i,j;
int a[10][10],n;
printf("Introdu n:");
scanf("%d", &n);
if (n > 10)
{
fprintf(stderr, "Invalid input %d\n", n);
return 1;
}
citMat(a,n);
for(i=0;i<n;i++){
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
return 0;
}
1. If you want to pass a 2-d array to function .Change your function definition to -
void citMat(int a[10][10], int n) { // first parameter to take a 2-d int array
2. And then inside function citMat to take input-
scanf("%d", &a[i][j]); // you need to write like this
Note -
1. Array indexing starts from 0 , so if you have array a[n] then it have valid index from 0 to n-1 .
So start reading from 0 and till n in all for loops . If you include n then you would access index out of bound and writing to it will cause undefined behaviour.
So, look out for that .
2. int main() -> int main(void) or int main(int argc,char **argv)
You need to change few things in your program to make it work
1) Call the function with the base address of the array, lik
citMat(a,n);
2) Change your function definition to,
void citMat(int a[10][10], int n)
to make it accept 2D array as parameter.
3) Change the scanf() to read for each element,
scanf("%d", &a[i][j]);
4) Since the array index starts from 0, change all the for loops termination condition to
for(i=1;i<n;i++)
Related
Code isn't done, so variables like KWHPrice can be ignored.
When I'm trying to run my code only the first print is displayed correctly, if I enter let's say 3 4 6 2 5, I get 4194432 (address), is suspect it's because I'm referring to int smallest wrong, as it's both a variable in main and in the function, hence two different variables. I would like some guidance
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
void GET_LOWEST_PRICE(double KWHPrice[MAX_SIZE], int i, double num, double smallest);
int main(void){
int num, smallest;
printf("\nEnter no of elements :");
scanf("%d", &num);
void GET_LOWEST_PRICE(double KWHPrice[MAX_SIZE], int i, double num, double smallest);
printf("\nSmallest Element : %d", smallest);
}
void GET_LOWEST_PRICE(double KWHPrice[MAX_SIZE], int i, double num, double smallest){
for (i = 0; i < num; i++)
scanf("%d", &KWHPrice[i]);
smallest = KWHPrice[0];
for (i = 0; i < num; i++) {
if (KWHPrice[i] < smallest) {
smallest = KWHPrice[i];
}
}
}
You are redeclaring GET_LOWEST_PRICE in main. To use it you will need to call it with your specified parameters.
ex: GET_LOWEST_PRICE(10.8, 1, 1.8, 0.2)
For homework I have to write a program where I am typing the string of n integer and then the program that prints the string,calculates the arithmetic mean and makes a new file only with even numbers and at the end prints the new file on screen.
And here are 2 programs
#include<stdio.h>
main()
{
int x,n,i;
FILE *p;
p=fopen("podaci.dat","wb");
printf("n=");
scanf("%d",&n);
fwrite(&n,sizeof(int),1,p);
for(i=0;i<n;i++)
{
printf("x=");
scanf("%d",&x);
fwrite(&x,sizeof(int),1,p);
}
fclose(p);
}
#include<stdio.h>
void stampa(int n,int a[])
{
int i;
for(i=0;i<n;i++)
printf("%5d",a[i]);
printf("\n");
}
float ars(int n,int a[])
{
int i,s=0;
float ars=0;
for(i=0;i<n;i++)
s+=a[i];
return 1.0*s/n;
}
main()
{
int i,n;
FILE *p,*u;
u=fopen("niz.dat","wb");
p=fopen("podaci.dat","rb");
fread(&n,sizeof(int),1,p);
printf("n=%d\n",n);
int a[n],m=0;
for(i=0;i<n;i++)
{
fread(&a[i],sizeof(int),1,p);
if(a[i]%2==0)
{
m+=1;
}
fwrite(&m,sizeof(int),1,u);
for(i=0;i<n;i++)
if(a[i]%2==0)
{
fwrite(&a[i],sizeof(int),1,u);
}
stampa(n,a);
printf("ars=%.2f",ars(n,a));
fclose(p);
fclose(u);
}
}
When I type more than 2 numbers the program instead of those numbers reads them as 0 and sometimes it adds numbers.
You cannot declare an array a[n] where n is a variable (number that you don't know at compile time but only once you read the file).
You can ether declare a[N] where N is a number big enough defined in with #DEFINE
#include <stdio.h>
#include ...
#DEFINE N 10000
//...
int a[N];
or you have to allocate a[] dynamically
int * a;
a = (int*)malloc(sizeof(int)*n);
You can read more about this here and here
It successfully compiles.
But at running on getting values of matrix it crashes stops working.
#include <stdio.h>
void getmat(int mat[100][100],int m,int n);
void matmul(int mat1[100][100],int mat2[100][100],int m1,int n1,int m2,int
n2,int matmul[100][100]);
void printmat(int matmul[100][100],int m,int n);
int main(void)
{
int m1,n1,m2,n2;
printf("Enter the dimensions of matrix1: ");
scanf("%d %d",&m1,&n1);
printf("Enter the dimensions of matrix2: ");
scanf("%d %d",&m2,&n2);
int mat1[m1][n1];
int mat2[m2][n2];
int matmul1[m1][n2];
int matmul2[m2][n1];
printf("For the values of matrix 1\n");
getmat(mat1,m1,n1);
printf("For the values of matrix 2\n");
getmat(mat2,m2,n2);
if(n1==m2)
{
printf("Mat1 x Mat2 is possible.");
matmul(mat1,mat2,m1,n1,m2,n2,matmul1);
printf("Mat1 x Mat2 :\n");
printmat(matmul1,m1,n2);
}
else
printf("Mat1 x Mat2 is not possible.\n");
if(n2==m1)
{
printf("Mat2 x Mat1 is possible.");
matmul(mat2,mat1,m2,n2,m1,n1,matmul2);
printf("Mat2 x Mat1 :\n");
printmat(matmul2,m2,n1);
}
else
printf("Mat2 x Mat1 is not possible.\n");
return 0;
}
void printmat(int matmul[100][100],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%3d ",matmul[i][j]);
}
printf("\n");
}
}
void getmat(int mat[100][100],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("Enter element of %dx%d: ",i+1,j+1);
scanf("%d",&mat[i][j]);
}
}
}
void matmul(int mat1[100][100],int mat2[100][100],int m1,int n1,int m2,int
n2,int matmul[100][100])
{
int i,j,k;
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
matmul[i][j]=0;
for(k=0;k<m2;k++)
{
matmul[i][j]+=mat1[i][k]*mat2[k][j];
}
}
}
}
Help making any changes or optimizing this code.
Also another way to this.
This error shows up at running a half
Use a Debugger
Also, all your functions are written to take a 100x100 matricies.
But you declare your matricies to have variable sizes:
int mat1[m1][n1];
int mat2[m2][n2];
int matmul1[m1][n2];
int matmul2[m2][n1];
When you pass a 3x3 matrix to a function that is expecting a 100x100 matrix, you will definitely have a bad time.
I see that you are setting up your arrays with variable sizes. You should really set it up as a very large array or use malloc to set up the array properly. When you call the processing routines, pass the arrays as pointers and use the sizes as arguments in order to set up your references. You should note that myarray[i, j] is the equivalent of a single valued array using i*rowsize + j. When you define your array in the subroutines as [100, 100], it will go to [i*100 + j] which is outside the bounds of your actual array.
This causes the program to crash.
Once you actually calculate everything properly, it should work.
as the title says, my program crashes when i try to print a bidimensional array.
The error is surely printf in function printarray, but i couldn't understand why it lead to crash.
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#define COL 40
#define ROW 40
void printarray(int array[COL][ROW], int col, int row);
main (){
int n,m,p,q;
int array[COL][ROW];
printf("Dammi 2 numeri \n"); scanf("%d",&n); scanf("%d",&m);
do{
printf("Dammi 2 p[<n] e q[<m] \n"); scanf("%d",&p); scanf("%d",&q);
}while(p>=n || q>=m);
printf("Mi hai dato: n= %d m= %d p= %d q= %d \n",n,m,p,q);
int i,j;
int random;
srand(time(NULL));
for(i=0; i<=n;i++){
for(j=0; j<=m;j++){
do{
random = rand() % 10;
}while(random == 0);
printf("\n i am at array[%d][%d] with number: %d\n",i,j,random);
array[i][j] = random;
}
}
//printf("lol0 ->>>>>>>>>>>%d<--------",array[0][0]);
printarray(array[n][m],n,m);
system("PAUSE");
}
void printarray(int array[COL][ROW], int col, int row){
int i,j;
for(i=0; i<=col;i++){
//printf("lol3 %d",i);
for(j=0; j<=row; j++){
printf("%d",array[i][j]);
}
printf("\n");
}
}
If an array dimension has size N then the valid range of indices is [0, N - 1].
So the loops in the function should look like
void printarray(int array[COL][ROW], int col, int row){
int i,j;
for(i=0; i< col;i++){
//printf("lol3 %d",i);
for( j=0; j<row; j++){
printf("%d",array[i][j]);
}
printf("\n");
}
}
The same is valid for loops in main.
This call of the function invalid
printarray(array[n][m],n,m);
There shall be
printarray(array,n,m);
Take into account that such a declaration of an array like
int array[COL][ROW];
is very confusing. It would be more correctly to write
int array[ROW][COL];
^^^ ^^^
Also it is not clear what is the meaning of variables p and q in this loop
does not make sense
printf("Dammi 2 numeri \n"); scanf("%d",&n); scanf("%d",&m);
do{
printf("Dammi 2 p[<n] e q[<m] \n"); scanf("%d",&p); scanf("%d",&q);
}while(p>=n || q>=m);
It seems that this loop is from some other program.:)
And you have to check that n and m are not greater than ROW and COL.
Also it is a bad idea to mix l'Italian with English.:)
The posted code does not cleanly compile, for several reasons.
1) missing
#include <time.h>
2) function main() ALWAYS returns 'int' so a proper main statement would be: 'int main( void )'
3) the first parameter to 'printarray()' is defined to be a pointer to a 2 dimensional array.
However,
a) the actual call passes the contents of a specific 'cell' in the array.
b) the passed 'cell' is beyond the end of the array.
Suggest: 'printarray( array, n, m )' as the calling statement.
Note, in C, an array name degrades to the address of the first entry in the array.
4) an array should be defined as arrayName[numRows][numColumns].
This will become much more important when declaring an array of pointers where each pointer will point to the contents of the associated row.
In the posted code, the definition/naming is backwards from typical.
in Memory, an array is laid out left to right (the columns) then top to bottom (the rows).
The 'biggest' index is rows and should be listed first.
5) to avoid text replacement problems, numeric values in macros should be wrapped in parens.
6) as a suggestion, appropriate vertical spacing (blank line) between code blocks makes the code much clearer and more understandable by us humans
7) when calling scanf() (and family of functions) always check the returned value (not the parameter values) to assure the operation was successful
8) consistent indentation makes the code much easier to read/understand by us humans, suggest: indent 4 spaces after every opening brace '{' and un-indent before every closing brace '}'. Note: Never use tabs for indenting. Different environments have different tab widths and/or different tab stops.
9) for readability, and to make debug much easier, only place one statement per line of code. This applies to declaring variables, so they can be easily commented and applies to executable statements
here i am attaching the your program without any error. the problem was to calling the printarray function.
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#define COL 40
#define ROW 40
void printarray(int array[COL][ROW], int col, int row);
main (){
int n,m,p,q;
int array[COL][ROW];
printf("Dammi 2 numeri \n"); scanf("%d",&n); scanf("%d",&m);
do{
printf("Dammi 2 p[<n] e q[<m] \n"); scanf("%d",&p); scanf("%d",&q);
}while(p>=n || q>=m);
printf("Mi hai dato: n= %d m= %d p= %d q= %d \n",n,m,p,q);
int i,j;
int random;
srand(time(NULL));
for(i=0; i<=n;i++){
for(j=0; j<=m;j++){
do{
random = rand() % 10;
}while(random == 0);
printf("\n i am at array[%d][%d] with number: %d\n",i,j,random);
array[i][j] = random;
}
}
//printf("lol0 ->>>>>>>>>>>%d<--------",array[0][0]);
printarray(array,n,m);
system("PAUSE");
}
void printarray(int array[COL][ROW], int col, int row){
int i,j;
for(i=0; i<=col;i++){
//printf("lol3 %d",i);
for(j=0; j<=row; j++){
printf("%d",array[i][j]);
}
printf("\n");
}
}
I want to make a program that uses a function I created where it swaps all the elements of an array X (that has the length of N) with some number K, only if that element is greater than K. Where am I going wrong here?
#include <stdio.h>
#include <stdlib.h>
int swap_K(int *, int);
int main()
{
int N,i,K;
printf("Enter N: ");
scanf("%d",&N);
printf("Enter K: ");
scanf("%d",&K);
int X[N];
for (i=1; i<=sizeof(X)/sizeof(int); i++){
printf("Enter %d. element: ",i);
scanf("%d",&X[i]);
}
swap_K(X,K);
for (i=1; i<=sizeof(X)/sizeof(int); i++){
printf("%d",X[i]);
}
}
int swap_K(int *X, int K)
{
int i;
for (i=1; i<=sizeof(X)/sizeof(int); i++){
if (X[i]>K)
X[i]=K;
}
return X;
}
In swap_K(int *X, int K), sizeof(X) is sizeof(int *), not the size of the array.
In C, a pointer is not really the same as an array.
To fix it, use N instead of sizeof(X)/sizeof(int) everywhere, esp. inside swap_K().
1) Arrays start with index 0.
2) In your main function you don't need to use sizeof(X)/sizeof(int) in for loop as you already know it is equal to N.
3) When you pass the array to the function, you are sending the base address of the array which decays into pointer, so in swap_K function, sizeof(X) will return sizeof(int) which is 4(generally).
To overcome this you should send the size of your array from main function. For example: swap_K(X,K,N);
4) You don't need to return X from swap_K as you are sending the base address of X from main function.
For example:
#include <stdio.h>
#include <stdlib.h>
int swap_K(int *, int, int);
int main()
{
int N,i,K;
printf("Enter N: ");
scanf("%d",&N);
printf("Enter K: ");
scanf("%d",&K);
int X[N];
for (i=0; i<N; i++)
{
printf("Enter %d. element: ",i);
scanf("%d",&X[i]);
}
swap_K(X,K,N);
for (i=0; i<N; i++)
{
printf("%d",X[i]);
}
}
int swap_K(int *X, int K,int N)
{
int i;
for (i=0; i<N; i++)
{
if (X[i]>K)
X[i]=K;
}
//return X; //This is not required
}
Your loop is incorrect
for (i=1; i<=sizeof(X)/sizeof(int); i++)
It should be
for (i=0; i<N; i++)
There are several problems with the code posted:
arrays in C are 0-indexed, so the for loops should ALWAYS iterate from 0 to N - 1. Iterating past N is a buffer overflow
the pointer to the array is just the pointer to the first element of the array. The swap function can't know if the pointer passed to it is part of an array or a single value. With this in mind it will need to take another argument which tells what is the size of the passed in array as pointer. Iteration inside the loop will use that value instead of sizeof(X) / sizeof(int) = 1
you're defining X as a variable sized array which is allocated entirely on the stack. Introducing a reasonably large N will crash your program. It would be better to allocate the array in the heap if you don't know what the size of the input will be.