I'm currently learning C and wanted to write a program that takes a number ganzeZahl to determine array length.
Then you have to input the numbers being stored in that array of size n and after that it's supposed to do a selection sort (which I cut out here, because my program doesn't even reach to that part).
I can't get past the while loop whenever I try to run it. It compiles fine.
It never reaches the printf("!!!--------!!!"); //this part isn't reached for some reason? test5.
#include<stdio.h>
int main() {
int ganzeZahl;
scanf("%d", &ganzeZahl);
//printf("ganze Zahl ist: %d\n", ganzeZahl); //test
int array[ganzeZahl];
int i = 0;
for(int z = 0; z < ganzeZahl; z++) {
printf("%d\n", array[z]);
}
while(i<ganzeZahl) {
//printf("!!!hier!!!\n"); //test2
scanf("%d", &array[i]);
//printf("zahl gescannt!\n"); //test 3
i++;
//printf("i erhöht!\n"); //test 4
}
printf("!!!--------!!!"); //this part isn't reached for some reason? test5
//selection sort here
//[...]
return 0;
}
Your program does execute correctly, and eventually reaches the last printf call.
When, it enters the whileloop, it keeps on calling scanf, what causes it to stop and wait until you enter a value every iteration. If you provide ganzeZahl inputs (enter a number and press 'enter'), it will complete the loop and proceed. I guess that if you add a printf before scanf within the loop, it should be more intuitive.
for(int z = 0; z < ganzeZahl; z++){
printf("%d\n", array[z]);
The array is not initialized and so you can't print the array yet.
Actually you messed up with the order of code.The while loop should come first and then the for loop. I have corrected your code below. Happy coding!
#include<stdio.h>
int main() {
int ganzeZahl;
scanf("%d", &ganzeZahl);
//printf("ganze Zahl ist: %d\n", ganzeZahl); //test
int array[ganzeZahl];
int i = 0;
while(i<ganzeZahl) {
//printf("!!!hier!!!\n"); //test2
scanf("%d", &array[i]);
//printf("zahl gescannt!\n"); //test 3
i++;
//printf("i erhöht!\n"); //test 4
}
for(int z = 0; z < ganzeZahl; z++) {
printf("%d\n", array[z]);
}
printf("!!!--------!!!"); //this part isn't reached for some reason? test5
//selection sort here
//[...]
return 0;
}
Related
I'm just learning programming with c.
I wrote a program in c that had a bug in the body of the while loop
I did not put {}.
The program is as follows, but the question that came to me later is how to run the program in c? Why does error 2 not print while it is before the start of the while loop? If the c language is a compiler, why is it that the error of the whole program is not specified first, and up to line 15 the program is executed without any problems?
int main()
{
int n ,k;
float d ;
printf("please write your arithmetic sequence sentences ");
scanf("%d",&n);
printf("\n");
printf("please write your common differences");
scanf("%d",&k);
printf("\n");
printf("please write your initial element ");
scanf("%f",&d);
printf("error 1");
printf("\n");
printf("error 2");
printf("number \t sum");
printf("erorr 3");
int i = 0;
int j = 0;
int sum = 0;
while (i < n)
j = d + i*k;
sum += j;
printf("%d\t%d",j,sum);
i++;
return 0;
}
Firstly, the program enters an infinite loop:
while (i < n)
j = d + i*k;
Since the values of i and n do not change, the condition never becomes false.
Secondly, the printing sequence:
printf("error 2");
printf("number \t sum");
printf("erorr 3");
does not display a line break at the end. The output is buffered (stored internally) waiting for the line break to be printed, which, naturally, never happens. Add \n at the end of "erorr 3" to see the difference.
#include <stdio.h>
int main()
{
int n ,k;
int i = 0;
int j = 0;
int sum = 0;
float d ;
// If the given values are not an integer, it wouldn't continue the sequence and end as an "error"
printf("please write your arithmetic sequence sentences ");
if(scanf("%d",&n)){
printf("please write your common differences");
if(scanf("%d",&k)){
printf("please write your initial element ");
if(scanf("%f",&d)){
printf("Number \tSum\n");
} else {
printf("error");
}
} else{
printf("error");
}
} else{
printf("error");
}
// This is where the values get solved
// You also forgot to add {} in your while statement
while (i < n){
j = d + i*k;
sum += j;
printf("%d\t%d",j,sum);
i++;
}
return 0;
}
I'm new to C and need some help to understand how this piece of code works. I know that it reads the values that the user writes, puts them into an array, and then prints them out.
But I don't understand why I need two "counters" (i and j) to do this. Can someone help me to figure it out?
#include<stdio.h>
int main ()
{
int A[5];
int i=0;
int j=0;
while (i < 5)
i++;
printf("Enter your %d number\n", i);
scanf("%d", &A[i]);
}
while (j < 5)
{
j++;
printf ("\n%d\n", A[j]);
}
}
Technically, what you have aren't two counters, but two loops. If you wanted, to, you could just reuse i for the second loop as well, by doing something like this:
while (i < 5)
i++;
printf("Enter your %d number\n", i);
scanf("%d", &A[i]);
}
i = 0;
while (i < 5)
{
i++;
printf ("\n%d\n", A[i]);
}
As for why you have two loops, the reason is simple. The first loop (using i in your code), reads the 5 integers into the array A. After the first loop concludes, your array A holds the 5 int values, which you could've used however you wanted. In your case, you want to print those values. So what you do is use a loop for looping over the array elements and printing the values to the screen, one by one.
You don't need it, you can simply reset the first and reuse it. However you must increment your index only after having using it otherwise you will overflow the limit of the array :
#include<stdio.h>
int main ()
{
int A[5];
int i=0;
while (i < 5) {
printf("Enter your %d number\n", i);
scanf("%d", &A[i]); // the last must be 4 not 5
i++; //<== increment here
}
i=0;
while (i < 5)
{
printf ("\n%d\n", A[i]); //idem
i++;
}
}
I want to write a loop that runs until the user enters a number greater than 10, but I have to do something wrong because it creates an infinite loop.
int main()
{
int a;
printf("Enter 'a' value (min 10): ");
scanf("%d",&a);
for(int i=0;a<10;i++){
printf("Enter value>10");
i++;
printf("%d",&a);
}
printf("Result:%d",a+a-2+a-4+a-6+a-8+a-10);
return 0;
}
You mix an index that does not make sense. Also you print the memory address of variable instead of its value, not sure it is what you wanted?
Code partially corrected (because I don't know what is your ultimate goal):
#include <stdio.h>
int main()
{
int a;
do {
printf("Enter 'a' value (min 10): ");
scanf("%d",&a);
printf("\na: %d\n",a);
} while (a <= 10);
printf("Result:%d\n",a+a-2+a-4+a-6+a-8+a-10);
return 0;
}
ps: \n is line return and added do while which is what you want when you want to execute a loop at least once.
Have a look at your for-loop: you let i start at zero, you continue until a is not smaller than ten anymore, but it's not the value of a you need to check, it's the one of i.
In top of that, you are doing a i++ within your for-loop, while this is already covered in the definition of the for-loop.
I think this is the code that you are looking for: See comments
#include <stdio.h>
int main()
{
int a, ok = 0, end_of_input = 0;
do {
printf("Please input an integer value (min. 10): ");
fflush(stdout); // So the user can see the above line!
switch(scanf("%d",&a)) {
case EOF: // End of input - Give up!
end_of_input = 1;
break;
case 1: // Got a number - Check it!
if (a < 10)
{
ok = 1;
} else {
printf("%d - Not appropriate input. Please try again.\n\n",a);
}
break;
default: // Summat else - "eat" the input to the next line
scanf("%*[^\n]\n"); // "eats" the rest of the line in the buffer w/o assignment
break;
}
} while (end_of_input == 0 || ok == 0);
if (ok) { // User entered a valid number
printf("Got a that is smaller than ten %d\n", d);
} else { // We have ran out of input
printf("See you want to leave us :-(\n");
}
return 0;
}
I am not sure what you are trying to achieve but one problem that I found in your logic is you prompting user for input outside the loop. So whenever you enter number less than 10 it always goes in infinite iteration.
Try following code, with scanf inside loop
int main()
{
int a;
printf("Enter 'a' value (min 10): ");
scanf("%d",&a);
int i=0;
for(;a<10;){
printf("Enter value>10");
scanf("%d",&a);
printf("%d",a);
i++;
}
printf("Result:%d",a+a-2+a-4+a-6+a-8+a-10);
return 0;
}
I am trying to get user input and store them in an array Fib[i]. After that print the Fib[i] array. When the user enters -1 the loop will quit and the program will end. But my code is not printing or terminating.
#include <stdio.h>
double Fib[50];//globally declared
int fib(int n)
{
int i;
for(i=0; i<50; i++)
{
scanf("%d", &Fib[i]);
if(i==-1)
break;
//printf("numbers entered %d\n", Fib[i]); // <- doesn't terminate if printf is here
}
printf("numbers entered %d\n", Fib[i]); //doesn't print anything??
}
int main()
{
int i, n;
//calling the function
fib(n);
return 0;
}
user input:
4
5
-1
Expected output:
Numbers entered
4
5
First issue: you declare Fib as an array of double:
double Fib[50];
But you use %d to read the values, which is for reading an int:
scanf("%d", &Fib[i]);
Using the wrong format specifier invokes undefined behavior. You presumably want to store integers, so change the array to int:
int Fib[50];
Next is your array breakout condition:
if(i==-1)
i is your array index, which ranges from 0 to 49, so this will never be true. You want to stop when the user enters -1, and that value will be in Fib[i]:
if(Fib[i]==-1)
Finally, printing the array:
printf("numbers entered %d\n", Fib[i]);
This doesn't print the array. It just prints the element at the last index of i, and the value at that index will always be -1. You need a separate loop to print the values:
int j;
printf("numbers entered:\n");
for (j=0; j<i; j++) {
printf("%d\n", Fib[j]);
}
This code has many code writing standard issues but it seems you are new so I am making minimal changes just for your understanding
#include <stdio.h>
double Fib[50];//globally declared
int fib(int n)
{
int i,j;
for(i=0; i<50; i++)
{
scanf("%lf", &Fib[i]);
if(Fib[i]==-1)
break;
}
printf("numbers entered \n");
for(int j=0;j<i;j++)
{
printf("%lf\n",Fib[j]);
}
}
int main()
{
int i, n;
fib(n);
return 0;
}
My sorting program results in a "segmentation fault 11":
#include <stdio.h>
int main()
{
// Asking user for number of inputs in an array
int n;
do {
printf ("enter the number of intigers you want to sort\n");
scanf("%d",&n);
}while (n<=1);
int sort [n];
printf ("please enter %d numbers\n",n);
for (int i=0; i<n; i++) {
scanf("%d",&sort[i]);
}
printf("you entered\n ");
for (int i=0; i<n; i++) {
printf(" %d ",sort[i]);
}
printf("\n");
int k,c,i,x;
for (i=0;i<n;i++) {
if (sort[i]<sort[i-1]){
k=i-2;
while (sort[k]>sort[i]){
k--;
}
k++;
x =sort[i];
c=i;
for (c=i;c>k;c++){
sort[c-1]=sort[c];
}
sort[k]=x;
}
}
printf ("Sorted numbers :-\n");
for (int i=0; i<n; i++) {
printf ("%d ",sort[i]);
}
printf ("\n");
return 0;
}
Now I have looked up the internet and found that it is caused because a variable has value that exceeds the system's memory limit. But cannot understand that concept.
for (i=0;i<n;i++)
{
if (sort[i]<sort[i-1])
You are accessing array out of its bounds.Maybe you want to start your loop from 1.Also
k=i-2;
while (sort[k]>sort[i])
will access index beyond 0 for example if i is 0 or 1 or 2
k = i - 2; looks unstable for low values of i.
sort[i - 1] is undefined when i is zero.
The thing causes the behaviour of sort[k] to be undefined.
Moral of the story: check all the array indexes before attempting to retrieve an array element. Your debugger will help you here.
In addition to all said before:
c=i;
for (c=i;c>k;c++){
sort[c-1]=sort[c];
}
sort[k]=x;
i can be 0 -> c can be 0 -> sort[c-1] would also result in accessing array out of bounds.
There is no need to initialize c=i twice. It is enough to do it in the loop-declaration