In my book , this code is given.They say that the output is 2 2 2 2 2 2 3 4 6 5
Please explain is this correct or not ? If not then what is the correct o/p?
#include <stdio.h>
#include <string.h>
main()
{
int c[]={2,8,3,4,4,6,7,5};
int j,*p=c,*q=c;
for(j=0;j<5;j++){
printf(" %d",*c);
++q;
}
for(j=0;j<5;j++){
printf(" %d",*p);
++p;
}
}
In first for-loop you are printing *c instead of *q:
printf(" %d",*c); // outputs `2 2 2 2 2` as first element, five times
should be:
printf(" %d",*q);
as I notice you increments q
output is 2 2 2 2 2 in first loop because of *c, c decays into address of fist element in this expression.
Edit
According to your code output should be as suggested by #ChronoTrigge (I notice latter):
First loop outputs five times 2 as I explained above
second loop will output first five elements in array a[] so output should be: 2 8 3 4 4
complete output: 2 2 2 2 2 2 8 3 4 4
Related
input: 'n' which will always be an odd number
output for n=3
1
2 4
3
output for n=5
1
2 4
3 5 7
6 8
9
Help me in understanding the logic.
What I could understood was that in each line the numbers are having a difference of 2.
If your task is to write a program to print those patterns, I suggest you start smaller. First write a program to print this pattern:
0 1 2
The outline of your program will look something like this:
#include <stdio.h>
int main()
{
int n = 3;
int i;
for(i = 0; i < 3; i++) {
// you fill in something here
}
}
That should be easy. Once you get that to work, make a simple modification so it prints this:
1 2 3
That should be easy. Once you get that to work, try writing a program to generate this pattern:
1
1 2
1 2 3
That should be pretty easy. Once you get that to work, try modifying it so it generates this pattern:
1
1 2
1 2 3
And once you get that to work, you can try printing the diamond patterns in your assignment.
the input depicts no of tests followed by size of input array followed by the array and the output either gives -1 or the square of the largest prime in the given array.
I am providing the code and the expected and actual output along with sample standard input used.
standard input:
3
5
1 4 6 8 10
3
2 2 9
2
156 13
expected output | getting
-1 -1
4 4
169 -1
#include <stdio.h>
int main(){
int test,size;
int i,j;
scanf("%d\n",&test);
while(test>=1){
scanf("%d\n",&size);
int data[size],factors=0,max=0;
for(i=0;i<size;i++){
scanf("%d ",&data[i]);
for(j=1;j<=data[i];j++){
if(data[i]%j==0){
factors+=1;
}
}
if((factors==2) && (data[i]>max)){
max=data[i];
}
}
if(max>=2){
printf("%d\n",max*max);
}else{
printf("%d\n",-1);
}
max=0;
test-=1;
}
}
ok debugged and got the answer. Factors needs to be initialized inside the next for loop.
User will give N,S respectively
If the value of N is 3, i.e i have 3 boxes and the input of S is 6.
Then I am coding in such a manner that in the 1st case(n=3), 1 is added to each box,then again(n=2) 1 is added to each box (A,B,C) excluding the last box, again(n=1) 1 is added to each box excluding the last box as well as the second last box.
And the total number of balls left in each box is to displayed after distribution of total value S,
For example :
It looks like this, where a,b,c are the 3 boxes.
n=3 and s=6
A B C
1 1 1
1 1
1
-----
3 2 1 //output to be displayed
again if the input of n ans s are 4 and 9
then output would be,
A B C D
1 1 1 1
1 1 1
1 1
-------
3 3 2 1 //output to be displayed
again if the input of n ans s are 3 and 4
then output would be,
A B C D
1 1 1 1
1
-------
2 1 1 1 //output to be displayed
again if the input of n ans s are 4 and 2
then output would be,
A B C D
1 1
-------
1 1 0 0 //output to be displayed
for n max value of s = n*(n+1)/2
Actually in this case the the complexity of the code cant be O(n^2). SO this was my code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i,j,s,y,x1;
scanf("%d%d",&n,&s);
int b[n];
memset(b,0,n);
x1=n;
j=0;
while(s>0)
{
y=j%n;
if(j!=n-1)
{
b[y]++;
s--;
j++;
}
else
{
b[y]++;
s--;
j=0;
n--;
}
}
for(i=0;i<x1;++i)
{
printf("%d ",b[i]);
}
return 0;
}
But after executing the code,The output given by my code:
where n = 4 and s = 9
the output is : 3 32631 -747449654 32629
where n = 3 and s = 5
the output is :2 2 4195586
Why is this happening ? I dont want to use nested for loops!
But whats wrong in here ? please help me!
void * memset ( void * ptr, int value, size_t num );
num is the number of bytes, so
memset(b,0,n);
should be
memset(b,0,n*sizeof(int));
Live Demo
The third argument to memset is the number of bytes to set. Since b is an array of int, it has 4*n bytes that need to be cleared, assuming 32-bit ints. The correct code is
memset( b, 0, sizeof(b) );
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.
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?