Wrong Output,but why? - c

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) );

Related

Printing Number Pattern in C

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.

Not getting desired output. Is there something inherently wrong in the logic?

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.

Pointers: why the output is 6?

The output of the program below is 6. I am not able to figure out why. When I trace it out by hand, I am getting 5.
#include<stdio.h>
#include<conio.h>
main()
{
int i,count=0;
char *p1="abcdefghij";
char *p2="alcmenfoip";
for(i=0;i<=strlen(p1);i++) {
if(*p1++ == *p2++)
count+=5;
else
count-=3;
}
printf("count=%d",count);
}
if(*p1++ == *p2++) is reading both p1 and p2 character by character. When the characters are the same, it will increase count by 5, else it will decrement it by 3. But, there is another thing that you didn't pay attention to: strlen(p1) will always be different in each iteration, because p1 will change. So, in each iteration, you also need to check its value.
p1 p2 count i strlen (before entering into the loop body)
a a 5 0 10
b l 2 1 9
c c 7 2 8
d m 4 3 7
e e 9 4 6
f n 6 5 5 <- No more - this is the last one
The trick here is, strlen(p1) changes every iteration. So loop condition goes
0 <= 10 +5
1 <= 9 -3
2 <= 8 +5
3 <= 7 -3
4 <= 6 +5
5 <= 5 -3
So equal characters are a, c, e, shown as +5 above. Total is 6.
Your program stops when i>strlen(p1) because you are changing p1 each time you do *p1++.
When it computes the condition strlen returns the size from the las char.
If you store the value in a variable at the beginning (before your loop) it should work.
Anyway, try to avoid pointer arithmetic...

I get an Unexpected Output

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

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