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

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.

Related

how to get sum of each input printed?

I am new to C and need some help with this basic program. I want the program to print out the sum directly after the input. I tried to put printf("..%d", sum); inside the for brackets and got but program shuts after the first input. It works if I have a scanf("%d", &value); before the For and inside the for brackets but there for I don't get the format I want.
I want the values to be printed vertical, left side should be inputs and right side should be output as u can see in the image.
Here is my code
#include <stdio.h>
int main()
{
int sum = 0, c, value;
scanf("%d", &value);
for (c = 1; c <= value; c++)
{
sum = sum + c;
printf("..%d", sum);
scanf("%d", &value);
}
return 0;
}
It appears you are a bit confused about which values you are actually summing (or I am...) From your problem descriptions (and from how you have initialized your for loop), it appears you are to input the ending value and then are to sum from 1 to that value?
If that is the case, there is no need for additional input within the for loop, your loop variable provides all values you are supposed to add to your sum.
Additionally, you cannot use any user-input function correctly unless you validate the return to determine whether input succeeded or failed.
Putting it altogether, you could do:
#include <stdio.h>
int main()
{
int sum = 0, c, value;
fputs ("sum from 1 to: ", stdout); /* prompt for input */
if (scanf ("%d", &value) != 1 || value < 1) { /* VALIDATE EVERY INPUT !!! */
fputs (" error: invalid integer input or negative value.\n", stderr);
return 1;
}
putchar ('\n'); /* add newline before output */
/* you are looping over the values of c -- no input is necessary */
for (c = 1; c <= value; c++)
{
sum = sum + c; /* sum */
printf ("%2d\t\t\t%d\n", c, sum); /* output value added and sum */
}
return 0;
}
Example Use/Output
$ ./bin/suminput
sum from 1 to: 5
1 1
2 3
3 6
4 10
5 15
Or for 1-10:
$ ./bin/suminput
sum from 1 to: 10
1 1
2 3
3 6
4 10
5 15
6 21
7 28
8 36
9 45
10 55
Or if invalid input is provided:
$ ./bin/suminput
sum from 1 to: twenty
error: invalid integer input or negative value.
Or a negative value:
$ ./bin/suminput
sum from 1 to: -10
error: invalid integer input or negative value.
No harm done.
Taking Input For The Sum
The other way I read your sample input and output (characterized by the 0 ending the loop), is that your task is simply to read inputs and sum them until the user enters 0 (or an invalid integer) displaying the sum after each input. That is quite a bit easier -- but the requirement to validate each input remains the same, e.g.
#include <stdio.h>
int main()
{
int sum = 0, value;
while (scanf ("%d", &value) == 1 && value != 0) { /* input value */
sum += value; /* sum */
printf ("%20d\n", sum); /* output sum */
}
}
Example Use/Output
$ ./bin/suminput2
1
1
2
3
3
6
4
10
5
15
0
Or any random values until 0 is entered:
$ ./bin/suminput2
1
1
-2
-1
3
2
-4
-2
5
3
-6
-3
0
(note: there are ways to place the input and output on the same line but that differs between compilers and operating systems. Windows could use the non-standard getch(), while on Linux you would need to do the same thing by changing input to non-cannonical mode with tcsetattr(). Another way would simply be to save the inputs until the user enters 0 and then to loop over all values input summing and displaying the values (though that would eliminate the 0 at the end of input show in your example input))
Look things over and let me know if you have further questions (and also whether I interpreted your question correctly). If you do need to enter each value to sum, let me know and I am happy to help further.

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

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.

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