it asks two questions at once,why?
here is my code:
#include <stdio.h>
int main()
{
int i,j;
int n;
int adjmatrix[n][n];
char ans;
printf("How many vertices?");
scanf("%d",&n);
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
printf("Are vertex %d and %d adjacent?",i,j);
scanf("%c",&ans);
if (ans=='Y' || ans=='y')
{
adjmatrix[i][j]=1;
}
else adjmatrix[i][j]=0;
}
}
return 0;
}
Thanks in advanceļ¼
You're right. The output is weird.
Your problem is scanf. Use scanf("%2c", ans);. Helped me.
But even then, i had the problem that the loops are not iterated
correctly.
Explanation:
The reason was the matrix. The matrix is not allocating any memory because its size if variable.
Therefore by setting an entry of the matrix to 1, this influences the variable j.
Solution:
You need to solve the problem to dynamically allocate memory.
Have a look at:
https://www.eskimo.com/~scs/cclass/int/sx9b.html
https://www.geeksforgeeks.org/dynamically-allocate-2d-array-c/
Related
I'm trying to sum columns in C. My code only adds the sum for the firsts columns for example if is a 2x2 array it only add the first column but for the rest it displays weird numbers. this is what i got so far. I haven't made the code for the rows yet. Thanks for your help.
#include <stdio.h>
int main(){
printf("Enter the number of columns");
int i;
scanf("%d", &i);
printf("Enter the number of rows");
int y;
scanf("%d", &y);
int r[i][y];
int a;
int b;
int columntotal[i],rowtotal[y];
for (a=0; a<i; a++){
for (b=0; b<y; b++){
scanf("%d",&r[a][b]);
}
}
//printing
for(a=0;a<i;a++)
{
for(b=0;b<y;b++){
printf("\t%d", r[a][b]);
}
printf("\n");
}
for (a=0;a<i;a++){
for (b=0;b<y;b++){
columntotal[a]=columntotal[a]+r[a][b];
}
}
for (a=0;a<i;a++){
printf("%d\n", columntotal[a]);
}
return(0);
}
As far as I remember dynamic rows,col assign while creating array is not possible in C.So your this statement int columntotal[i],rowtotal[y]; will not work unless i,y is a constant variable i.e #define i,y,etc which is not in your case.
Second you are iterating over a columntotal array which is not properly declared. You can make use of dynamic memory allocation to allocate number of blocks to columntotal array and then fill it with you column sum as you iterate over it.
int *columntotal= malloc(i * sizeof (int));
for (a=0;a<i;a++){
for (b=0;b<y;b++){
columntotal[a]=columntotal[a]+r[a][b]; //r is your 2D array
}
}
I am a complete beginner in C and I am practicing passing arrays into functions. I wrote a program to take a two dimensional array as input and find sum of the individual columns.
And when I compiled the program I got no errors, but once I run it, I get a dialogue box saying "untitled5.exe stopped working" where untitled5 is the file name.
I got this error quite a few times. I have used both dev c++ and codeblocks to compile my program, so what is the reason for this? Is this a problem with my code or with my compiler or with my laptop?
#include<stdio.h>
void summation (int arr[][5], int size);
int main()
{
int n,arr[n][5],sum,i,j;
printf("enter the number of rows");
scanf("%d",&n);
for (i=0;i<n;i++)
{
for (j=0;j<5;j++)
{
printf("%d,%d th element is",i,j);
scanf("%d",&arr[i][j]);
}
}
summation (arr,5);
return 0;
}
void summation (int arr[][5], int size)
{
int i,j,s=0;
for(j=0;j<5;j++)
{
for (i=0;i<5;i++)
{
s=s+arr[i][j];
}
printf("%d",s);
}
}
In main() you are using i to index the first dimension of the array. In summation() you are using i to index the second dimension of the array. I think that you are going beyond the end of the first dimension inside summation() when main() does not fill up that much of the array (e.g., when you enter 2 for the number of rows).
I think you want
summation (arr,5);
And, inside summation():
for (i=0;i<size;i++)
{
s=s+arr[i][j];
}
#include<stdio.h>
void summation (int arr[][5], int size, int rows);
int main()
{
int n, sum, i, j;
printf("enter the number of rows");
scanf("%d",&n);
int arr[n][5];
for (i=0;i<n;i++)
{
for (j=0;j<5;j++)
{
printf("%d,%d th element is",i,j);
scanf("%d",&arr[i][j]);
}
}
summation (arr, 5, n);
return 0;
}
void summation (int arr[][5], int size, int rows)
{
int i,j,s=0;
for(i=0;i<rows;j++)
{
for (j=0;i<size;i++)
{
s=s+arr[i][j];
}
}
printf("%d",s);
}
So first off I moved your array declaration to after you have initialized n and made it equal to something.
Then your next problem was you were probably going out of bounds in your summation function. You always have 5 columns in your 2darray, but you can have a different amount of rows. Pass the amount of rows, n, into the function summation to make sure you don't go out of bounds.
I want to find the minimum number and summary from a dynamic integer table. I do not know why results not shown. Have I done something wrong on the malloc ? May I use realloc too ?
#include <stdlib.h>
#include <stdio.h>
int main()
{
int n,i,min,sum,xronos;
int* array;
printf("Give me how many numbers does the table will have: \n");
scanf("%d",&n);
array=(int*)malloc(n*sizeof(int));
for(i=1;i<=n;i++)
{
printf("Give the number %d",i);
printf("\n");
scanf("%d",&array[i]);
}
for(i=1;i<=n;i++)
{
sum=sum+array[i];
if (i=1)
{
min=array[i];
}
else
{
if (array[i]<min)
{
min=array[i];
}
}
}
printf("%d",sum);
printf("\n The answer is :",(n-2)*min+(sum-min));
getch();
return 0;
}
Yes, that is almost exactly how you are supposed to use malloc, except for three small things and one big thing:
Do not cast malloc result in C,
Use indexes from zero to n-1, inclusive (your code goes from one to n, inclusive)
Add a call free(array) to avoid a memory leak.
The big thing is that you do not need malloc in order to solve this problem: you can calculate the sum and the min as you go, without saving the individual items to an array.
You can replace the chain of ifs in the loop with this check:
if (i == 0 || array[i] < min) {
min=array[i];
}
This covers both the assignment of the first element, and comparison of elements other than the first one.
Finally, you can rewrite this
sum=sum+array[i];
as
sum += array[i];
using a composite assignment operator. Don't forget to initialize the sum to zero!
#include <stdlib.h>
#include <stdio.h>
#include <errno.h> /* Defines: ENOMEM */
int main()
{
Added rCode to assist in error handling.
int rCode=0;
Removed xronos as it is not used.
int n,i,min;
Initialized sum to 0;
int sum=0;
Initialize array pointer to NULL so cleanup is easier.
int *array = NULL;
printf("Give me how many numbers does the table will have: \n");
scanf("%d",&n);
There is nothing inherently wrong with casting the output of malloc to (int *). In some situations, it is a good coding practice. (good job).
array=(int*)malloc(n*sizeof(int));
You should always test that you actually got memory from malloc.
if(NULL == array)
{
rCode=ENOMEM;
fprintf(stderr, "malloc() failed.");
goto CLEANUP;
}
The array element index is in the range of 0 through (n-1). Start indexing at zero, and be sure to stop at (n-1).
for(i=0;i<n;i++)
{
printf("Give the number %d",i);
printf("\n");
scanf("%d",&array[i]);
}
Again, the array element index is in the range of 0 through (n-1). Start indexing at zero, and be sure to stop at (n-1).
for(i=0;i<n;i++)
{
sum=sum+array[i];
if(1 == i)
min=array[i];
else
{
if(array[i] < min)
min=array[i];
}
}
printf("%d",sum);
printf("\n The answer is :",(n-2)*min+(sum-min));
getch();
CLEANUP:
From my early years, my father would tell me: 'When you bring your toys to the sandbox, always remember to put them away when you are finished.'
if(array)
free(array);
return(rCode);
}
Line 22, need
if (i=1)
set to
if (i==1)
Right now you're setting i equal to 1
I am trying to solve this question, but on codechef its showing wrong answer,link to the queston is http://www.codechef.com/problems/STATUES/ .On system ,its showing correct answer, after trying a lot, i couldnt find the bug My code is,
#include<stdio.h>
//#include<conio.h>
int main()
{
int a[150];
int n;
int i;
int sum;
int avg;
int count=0;
scanf("%d",&n);
int ans;
int diff;
while(n!=0)
{
sum=0;
ans=0;
count++;
for(i=0;i<n;i++)
{ scanf("%d",&a[i]);
sum=sum+a[i];
}
avg=sum/n;
for(i=0;i<n;i++)
{ diff=avg-a[i];
if(diff>0)
ans=ans+diff;
}
printf("Set#%d\nThe minimum number of moves is %d.\n",count,ans);
scanf("%d",&n);
}
return 0;
}
Read the condition in the question:
Output a blank line after each test case.
Be careful about the case and punctuation in the above strings. Not adhering to the output format strictly will lead to the Wrong Answer verdict.
There is no blank line after your each output. Also one more for loop is needed to met the condition for exact output format. Add this snippet after while.
for(i=0;i<n;i++)
{
printf("\nSet #%d\nThe minimum number of moves is %d.\n",i,ans);
}
Another problem is
int a[150];
a should not exceed from 50. Change it to
int a[50];
But I woul advice you to use variable length array in this case to save memory.
I'm trying to make the letters of a matrix sort alphabetically and then be written out in a single string.For instance you type ten words,which are then stored in an array,and every letter has its place in the matrix then,right?But after I've written the words I want to bunch all the letters of all words together and then type all the letters out in alphabetical order.This is what I have so far:
#include <stdio.h>
#include <conio.h>
int main(void){
int i, j, k, f, n, m;
//was trying out various things,that's why I have so many useless ints up there
char word[10][15],temp;
for(i=0;i<=9;i++)
{
printf("Type in wword number %d: ", i+1);
gets(word[i]);
}
for(k=i-1;k>=0;k--)
{
for(m=0;m<k;m++)
if(word[k][f] > word[m][n])
{
temp=word[k][f];
word[k][f]=word[m][n];
word[m][n]=temp;
}
}
printf("Letters alphabetically sorted: ");
for(i=0;i<=9;i++){
for(j=0;j<=14;j++){
printf("%d",word[i][j]);
}
}
printf("\n");
getch();
}
I'm still in the process of learning about matrixes and I've gotten pretty familiar with arrays by now.But the sorting thing is confusing me,this was my attempt but it doesn't work.It lets you write all the words,and then it crashes.
What am I doing wrong here?And how do I correct it?
In your code here:
temp=word[k][f];
word[k][f]=word[m][n];
word[m][n]=temp;
the variables n and f are used uninitialised. That will most likely be the cause of the crash.
f,n are uninitialized. It has garbage and is the reason for crashing at this point.
for(k=i-1;k>=0;k--)
{
for(m=0;m<k;m++)
if(word[k][f] > word[m][n]) // f,n are uninitialized and are error prone
I think this will work..Please excute and tell me..
void main()
{
char word[10][15],temp,sorted_word[15];
int i,j,ii,k,l=0;
for(i=0;i<=9;i++)
{
printf("Type in wword number %d: ", i+1);
gets(word[i]);
}
for(i=0;i<=9;i++)
{
for(j=0;word[i][j]!='\0';j++)
{
ii=i;
for(k=j+1;1;k++)
{
if(ii==9 && word[ii][k]=='\0')
break;
if(word[ii][k]=='\0')
{
ii++;
k=0;
}
if(word[i][j]>word[ii][k])
{
temp=word[i][j];
word[i][j]=word[ii][k];
word[ii][k]=temp;
}
}
sorted_word[l++]=word[i][j];
}
}
sorted_word[l]='\0';
printf("%s",sorted_word);
getch();
}
here the
for(i=0;i<=9;i++)
{ printf("type in wword %d: ",i+1);
gets(word[i]);
}
gets (word[1]);
stores the value from word[1] onwards but where as the character array starts from
word[0].
may be this is not the full solution for u problem
this issue may help u in solving your doubt.