Multidimensional array: printing addition table in C - c

int main(){
int row=0,col=0;
printf("Enter number of rows of the table\n");
scanf("%d",&row);
printf("Enter number of columns of the table\n");
scanf("%d",&col);
printTable(row,col);
}
void printTable(int row,int col){
int i =0,j=0,k=1,L=1,num=0;
printf("row: %d, col: %d\n",row,col);
int table[row][col];
for (i;i<row;i++){
for (j;j<col;j++){
table[i][j] = i+j;
printf("%d ", table[i][j]);
}
printf("\n");
}
}
Trying to print an addition table using multidimensional array, my output looks like this:
row: 4, col: 4
0 1 2 3
But I'm supposed to get
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6
Please help me get right output. thanks

for (j;j<col;j++){
You aren't resetting j to zero before the second and subsequent passes. Change that to
for (j=0;j<col;j++){
You might want to change the other for-loop (on i) as well. Generally, unless you have a Very Good Reason for not doing so, you should always be initializing the loop variable before the first pass; that's why that first clause of the for syntax exists, after all.
By the way, if you didn't want to initialize the loop variable, you could have written this as for (;j<col;j++){ -- as you wrote it, the j is conceptually just retrieving that variable's value and throwing it away, and the for permits just leaving the initization, test, and/or update clauses empty if you don't need them. In fact, for(;;) { means the same thing as while(true) {, loop forever... and some people like to set up a macro,
#define EVER ;;
just so they can write the cutesy for(EVER) {

If you want output like
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6
Then following code prints the same outpit
for (i=0;i<row;i++){
for (j=0;j<col;j++){
table[i][j] = i+j;
printf("%d ", table[i][j]);
}
printf("\n");
}

Related

I tried make a bubble sort with C but it's not working (It's doing bubble sort 10 times)

first i giving how many numbers will i enter then i enter numbers. he need make bubble sort to them but its writing like======>
1 3 2 6 = 0 0 0 0(but it must be like 1 2 3 6(small to big))
the app how i want=
7
1 5 2 7 4 7 3
1 2 3 4 5 7 7
#include<stdio.h>
int main()
{
int numbers[500000];
int counter=0;
int howmany;
scanf("%d",&howmany);//getting how many numbers will we enter
int howmany2=howmany;//we will use this for write all numbers after all
while(howmany>0)//getting all numbers
{
scanf("%d",&numbers[counter]);
howmany--;
counter++;
}
int checker1,checker2;//its gonna check first and second number, then second and third...etc
int hm=howmany-1;//its gonna check entered number-1 times(1,2,3)={1,2},{2,3}
int clone1;//later we will copy numbers[checker1]
int tentime=10;//we gonna do bubble sort 10 times
while(tentime>0)//doing bubble sort
{
checker1=0;
checker2=1;
while(hm>0)
{
if(numbers[checker1]>numbers[checker2])
{
clone1=numbers[checker1];
numbers[checker1]=numbers[checker2];
numbers[checker2]=clone1;
}
checker1++;
checker2++;
hm--;
}
tentime--;
}
int counter2=0;
while(howmany2>0)//showing new number sort on screen
{
printf("%d ",numbers[counter]);
howmany2--;
counter2++;
}
printf("\n");
return 0;
}
You have several problems in your code:
At the end of your first while loop howmany will be 0. As a result hm will be set to -1 and the sort loop (while hm > 0 ) will never run.
When you print out the results are using counter as the array index (this is 4 which is out of bounds and never changes since you are incrementing counter2. As a result you are printing out an undefined value (0 in your case) four times.
Declaring an array of size 500000 may blow up your stack. Even if not it is way larger than you need

Error in C code while using 2d array

I am trying to implement this simple code(no algo as such).
#include<stdio.h>
#include<stdlib.h>
int main()
{
int l,h1,h2,h3,i,j,t,m,n;
scanf("%d %d",&m,&n);
int A[m][n];
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
scanf("%d",&A[i][j]);
}
int n1=m*n;
int adj[n1][n1];
printf("Before initialization array A =\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
printf("%d ",A[i][j]);
printf("\n");
}
for(i=1;i<=n1;i++)
{
for(j=1;j<=n1;j++)
adj[i][j]=-1;
}
printf("After initialization array A =\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
printf("%d ",A[i][j]);
printf("\n");
}
return 0;
}
This is my input file -
4 3
2 3 2
2 5 1
5 3 1
3 1 1
So here 1st 2 elements are m,n i.e. no of rows and columns.Then for m rows there are n elements.I store these elements in a 2d array A. I then print this array.It gives the correct answer.Then I am making new arrary adj[m*n][m*n].I initialize this array to -1.After that when I print my A array back first five element of A also becomes -1.I am not changing value of A.So why is this happening.This is the output I get-
Before initialization array A =
2 3 2
2 5 1
5 3 1
3 1 1
After initialization array A =
-1 -1 -1
-1 -1 1
5 3 1
3 1 1
C uses 0 based indexing. So, the valid indices of an array start from 0 and end at length-1.
This means that you'll have to modify your loops. This:
for(i=1;i<=m;i++)
and needs to be
for(i=0;i<m;i++)
You'll need to do the same for the all the other loops. Otherwise, you access an invalid array index and this leads to Undefined Behavior.
What you need to know is that in C/C++ array starts from 0 to array length -1. Change this in for loops and see what happens

Populate a 2D Array by a rule

How do I populate an array like this:
1 2 3 4
2 3 4 3
3 4 3 2
4 3 2 1
I need to find out a formula that determines the pattern of populating this array.
#include <stdio.h>
#define N 4
int main()
{
int i,j,arr[N][N];
int a=1;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
{
arr[j][i]=i+j+1;
}
for(i=0; i < N; i++)
for(j=0;j<N;j++)
printf("%2i ",arr[j][i]);
printf("\n");
return 0;
}
It prints out similarly to the desired array except that I need "3" at the end of the second row, and after that point, it goes reversed. Please, explain me how to do that.
Try this:
arr[j][i]=N-abs(i+j-(N-1));
abs() can be used any time you need a numerical sequence which is mirrored around some value. You just need to subtract a constant such that the value you want to mirror around is zero, take the absolute, and then re-adjust the output.
In your case (with N = 4) the (i+j) summation produces: 0,1,2,3,4,5,6. The middle value is N-1, as the largest value is 2 * (N-1).
Subtracting N-1 (3) gives: -3,-2,-1,0,1,2,3.
The abs() gives: 3,2,1,0,1,2,3.
If we subtract that from N (4) we get the desired 1,2,3,4,3,2,1 sequence.
arr[j][i]=(j * 3 + j/ 3 + b) % N + 1;

How could I circularly shift each row of an 2-d array by two positions?

First of all I want to show you what actually I want........
if input is ...
2 3 4
5 6 6
7 5 4
the output should be ...
7 5 4
2 3 4
5 6 6 /*Each row is shifted circularly left by two positons */
I tried this code acc. to my knowledge (I am a beginner in C) and have written this thing ..
/*To shift row of a 4 * 5 matrix by 2 positons left*/
#include<stdio.h>
int main() {
int a[4][5],i,j,k,(*temp)[5];
for(i=0;i<=3;i++) {
for(j=0;j<=4;j++)
scanf("%d",*(a+i)+j);
}
for (k=1;k<=2;k++) {
for(i=0;i<=3;i++) {
temp = (a+i); /*I thought that *(a+i) will point to the address of each row and so I should take it in a variable which is capable of pointing to a row of 5 variables that why TEMP */
(a+i) = (a+i+1);
(a+i+1) = temp;
}
}
for(i=0;i<=3;i++) {
for(j=0;j<=4;j++)
printf("%d\t",*(*(a+i)+j));
printf("\n");
}
return 0;
}
where am I wrong.....Please correct me ????
Your sample output looking like shifted along column :)
scanf("%d",*(a+i)+j); is not a good way, use
scanf("%d",&a[i][j]); instead
You tried to copy a row at temp = *(a+i);, but you can only
copy adresses here. temp is going to point a[i], but won't copy
it's data.
This code below gives
input
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
output
3 3 3 3 3
4 4 4 4 4
1 1 1 1 1
2 2 2 2 2
I have shifted columns like your sample and used a new array b instead of temp
#include<stdio.h>
int main() {
int a[4][5],i,j,b[4][5];
for(i=0;i<=3;i++) {
for(j=0;j<=4;j++)
scanf("%d",(*(a+i)+j));
}
for(i=0;i<=3;i++)
for(j=0;j<=4;j++)
{
*(*(b+i)+j)=*(*(a+((i-2+4)%4))+j);
}
for(i=0;i<=3;i++) {
for(j=0;j<=4;j++)
printf("%d\t",*(*(b+i)+j));
printf("\n");
}
return 0;
}
Although the concept that you are driving at could be made to work (but there is a LOT wrong with it at the moment) using pointer arithmetic in this context makes the code look very complicated so I wonder why you don't try to rewrite this using array syntax.
For example you could write your output like this:
for(i=0;i<=3;i++)
{
for(j=0;j<=4;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
I think this is the easier syntax for a beginner to understand. Similarly the row cycle / swap is far more transparent in this form.

Error reading test cases from a file in C

for a programming homework, I'm implementing Prim's algorithm, the format of the input file for test cases is as follows:
The first line of input will be an integer C, which indicates the number of test cases. The first line of each test case contains two integers N and E, where N represents the number of nodes in the graph and E the number of edges, respectively. Then come E lines, each with 3 integers I, J and P, where I and J represent the nodes of an edge (undirected graphs, where 0 ≤ I, J
Although even I'm starting the code (I'm new to programming) i Don´t understand why my code only reads an entry for the test cases, What am I doing wrong?
this is the code reading the file entradaA.in:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv []){
int testCases;
int numberNodes;
int numberEdges;
freopen("entradaA.in", "r", stdin);
int i, j, cont=1;
int total = 0;
int a, b, c;
scanf ("%d", &testCases);
for (i=0; i<testCases; ++i)
{
scanf ("%d %d", &numberNodes, &numberEdges); //Number Nodes & Edges
for (i=0; i<numberEdges; i++)
{
scanf ("%d %d %d", &a,&b,&c);//
printf ("%d %d %d\n", a, b, c);
}
printf ("Caso %d: Total Weight %d\n", cont++, total);
}
return (0);
}
The input file (entradaA.in) look something like this
2
7 11
0 1 17
0 2 10
0 6 14
1 2 6
1 3 1
2 3 4
2 6 3
3 4 7
4 6 10
4 5 2
5 6 9
6 9
0 1 30
0 2 30
1 3 22
1 5 33
2 3 20
2 4 33
3 4 15
3 5 20
5 4 20
You have the loop variable i modified inside the loop, which is generally unwanted. In this case, since i looped to 11 (the number of edges), it caused your program to terminate since 11 is not smaller than 2 (the number of test cases in the input).
You could use temporary variable (if you are using C++, thank you aardvarkk):
for (int i=0; i<testCases; ++i)
{
scanf ("%d %d", &numberNodes, &numberEdges); //Number Nodes & Edges
for (int j=0; j<numberEdges; j++)
Note that the int j could also be int i, but I wouldn't recommend it. Just use a variable with another name would be clearer. Or if you are in C, just drop the two int and use variables that are local to the function.
For more, you could read this.
Your code produced the following output on my machine. The only change I made was to declare the int values i, j etc. before the freopen call to make the code standard C.
0 1 17
0 2 10
0 6 14
1 2 6
1 3 1
2 3 4
2 6 3
3 4 7
4 6 10
4 5 2
5 6 9
Caso 1: Total Weight 0
It's definitely reading more than your test cases, so I'm not sure what the problem is?

Resources