C program not reaching For loop - c

I'm currently trying to write a C program using the NetBeans IDE.The goal is to take input from the user on the amount of sticks they want drawn on the screen where the Letter "I' is a stick as noted in the for loops. The thing is, I use scanf to get the user input for each row , but the program executes and runs but never actually seems to reach the for loop, since it doesn't do any of the print statements within it. My IDE doesn't print any of my print statements in the for-loops, which is why I'm assuming it isn't reaching them.
I have tried simply assuming values for user input, instead of using scanf, but that also does not seem to reach the for loop. Any input on what I could do to fix this, would be much appreciated!
#include <stdio.h>
int main(void) {
int length1, length2, length3;
int inputRow1, inputRow2, inputRow3;
printf("Please enter the number of Sticks you would like per row\n");
scanf("%d%d%d", inputRow1, inputRow2, inputRow3);
for (length1 = 0; length1 < inputRow1; length1++) {
printf("I");
for (length2 = 0; length2 < inputRow2; length2++) {
printf("I");
for (length3 = 0; length3 < inputRow3; length3++) {
printf("I");
return (0);
}
}
}
}

While using scanf you should store the values in the variables using &.
Change
scanf("%d%d%d",inputRow1,inputRow2,inputRow3);
to
scanf("%d%d%d",&inputRow1,&inputRow2,&inputRow3);
And your return statement should be before exiting from the main().
For example:
#include <stdio.h>
int main()
{
int length1,length2,length3;
int inputRow1,inputRow2,inputRow3;
printf("Please enter the number of Sticks you would like per row\n");
scanf("%d%d%d",&inputRow1,&inputRow2,&inputRow3);
for(length1=0;length1<inputRow1; length1++)
{
printf("I");
for(length2=0;length2<inputRow2;length2++)
{
printf("I");
for(length3=0;length3<inputRow3;length3++)
{
printf("I");
}
printf("\n");
}
printf("\n");
}
printf("\n");
return(0);
}

Related

My function is not executed during the code run

My code has 3 functions doing 3 different works. First one just prints "Hello!", second one takes an integer input from the user and returns its square and the third one takes a character input from the user and makes a triangular pattern out of it. But when I execute the code the first two functions work well and the third one doesn't. Although, when I execute the third function separately, it works well. What could be the problem?
Here is the code :
#include <stdio.h>
void greet()
{
printf("Hello !\n");
}
int square()
{
int n;
printf("Enter a number to be squared:\n");
scanf("%d",&n);
return n*n;
}
void pattern(char ch)
{
printf("Enter a Character:\n");
scanf("%c",&ch);
for (int i=10;i>0;i--)
{
for (int j=0;j<i;j++)
{
printf("%c",ch);
}
printf("\n");
}
}
int main()
{
int num;
char c;
greet();
num=square();
printf("%d\n",num);
pattern(c);
return 0;
}
Because a user is hitting the "Enter" key to submit their character for the second function, that "Enter" character is being passed on to the third function's scanf, and as such will print a triangle of new-lines (or a column in this case, you probably got a large amount of whitespace printed after the program's execution).
You can fix it by including a space before the %c in the pattern func's scanf, like this:
void pattern(char ch) {
printf("Enter a Character:\n");
scanf(" %c",&ch);
for (int i=10;i>0;i--)
{
for (int j=0;j<i;j++)
{
printf("%c",ch);
}
printf("\n");
}
}

C Loop until the condition is met problem

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

Trying to get a REPEAT UNTIL (DO...WHILE) loop working in C

So I am writing this code for simple shape determining program, and I was trying to loop it, the condition, for the loop to end is when the value of the variable x is equal to 0 (all of teh variables are angels), since an angle cannot be 0, I think it is only logical to have the loop loop until the value entered is 0. However, the code does not seem to stop iterating even after the condition is met. the code is below:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x=0;
int y=0;
int A=0;
do {
printf("What is the value of x?");
scanf("%d", &x);
printf("What is the value of y?");
scanf("%d", &y);
printf("What is the value of A?");
scanf("%d", &A);
if ((A==90)&&(x==y))
{
printf("Square\n");
}
else if ((A==90)&&(x!=y))
{
printf("Rectangle\n");
}
else if ((A==60)||(A==120))
{
printf("Hexagonal\n");
}
else if ((A!=60)||(A!=120)&&(x==y))
{
printf("Rhombic\n");
}
else if ((A!=60)||(A!=120)&&(x!=y))
{
printf("Parallelogram\n");
}
else
{
printf("The values you have entered are not supported by the program,
try again!");
}
}while(x!=0);
printf("Thanks for using the program!");
system("pause");
return 0;
}
I cannot really see what the problem with the condition for the while loop is, please help!
The easiest way may be:
scanf("%d", &x);
if (!x) break; // break the loop, not asking anything else

Counting arguments passed to scanf() in C

Is there any way to Count number of arguments passed to scanf() in C ? Specially, while assigning int arrays through scanf().
Example:
int array[1000], i;
for(i=0;i<1000;i++)
scanf("%d",&array[i]);
I need to count how many values are inserted by user
I don't think there's a built in way to do this, but why not just create a counter that increments when scanf returns successfully and break the loop otherwise?
int scanf_counter = 0;
int array[1000], i;
for(i=0;i<1000;i++) {
if(scanf("%d",&array[i] > 0) {
scanf_counter++;
} else {
break;
}
}
Although I'm not sure I understand your question exactly because you could always just find the size of the array by doing this
int size = sizeof(array)/sizeof(array[0])
Look the scanf() fragment carefully, thus:
include
int main()
{
double a[100000],mx=0;
int i,j,c=0;
printf("Enter as many numbers as you wish . Press Ctrl+D or any character to stop inputting :\n");
for(i=0;i<100000;i++)
{
if((scanf("%lf",&a[i]))==1)
c++;
//else break;
}
for(j=0;j<c;j++)
{
if(a[j]>mx) mx=a[j];
}
printf("You have inserted %d values and the maximum is:%g",c,mx);
return 0;
}

Sorting letters C programming

I'm trying to make the letters of a matrix sort alphabetically and then be written out in a single string.For instance you type ten words,which are then stored in an array,and every letter has its place in the matrix then,right?But after I've written the words I want to bunch all the letters of all words together and then type all the letters out in alphabetical order.This is what I have so far:
#include <stdio.h>
#include <conio.h>
int main(void){
int i, j, k, f, n, m;
//was trying out various things,that's why I have so many useless ints up there
char word[10][15],temp;
for(i=0;i<=9;i++)
{
printf("Type in wword number %d: ", i+1);
gets(word[i]);
}
for(k=i-1;k>=0;k--)
{
for(m=0;m<k;m++)
if(word[k][f] > word[m][n])
{
temp=word[k][f];
word[k][f]=word[m][n];
word[m][n]=temp;
}
}
printf("Letters alphabetically sorted: ");
for(i=0;i<=9;i++){
for(j=0;j<=14;j++){
printf("%d",word[i][j]);
}
}
printf("\n");
getch();
}
I'm still in the process of learning about matrixes and I've gotten pretty familiar with arrays by now.But the sorting thing is confusing me,this was my attempt but it doesn't work.It lets you write all the words,and then it crashes.
What am I doing wrong here?And how do I correct it?
In your code here:
temp=word[k][f];
word[k][f]=word[m][n];
word[m][n]=temp;
the variables n and f are used uninitialised. That will most likely be the cause of the crash.
f,n are uninitialized. It has garbage and is the reason for crashing at this point.
for(k=i-1;k>=0;k--)
{
for(m=0;m<k;m++)
if(word[k][f] > word[m][n]) // f,n are uninitialized and are error prone
I think this will work..Please excute and tell me..
void main()
{
char word[10][15],temp,sorted_word[15];
int i,j,ii,k,l=0;
for(i=0;i<=9;i++)
{
printf("Type in wword number %d: ", i+1);
gets(word[i]);
}
for(i=0;i<=9;i++)
{
for(j=0;word[i][j]!='\0';j++)
{
ii=i;
for(k=j+1;1;k++)
{
if(ii==9 && word[ii][k]=='\0')
break;
if(word[ii][k]=='\0')
{
ii++;
k=0;
}
if(word[i][j]>word[ii][k])
{
temp=word[i][j];
word[i][j]=word[ii][k];
word[ii][k]=temp;
}
}
sorted_word[l++]=word[i][j];
}
}
sorted_word[l]='\0';
printf("%s",sorted_word);
getch();
}
here the
for(i=0;i<=9;i++)
{ printf("type in wword %d: ",i+1);
gets(word[i]);
}
gets (word[1]);
stores the value from word[1] onwards but where as the character array starts from
word[0].
may be this is not the full solution for u problem
this issue may help u in solving your doubt.

Resources