I'm just trying to get this program to take a number between 1 and 9 and calculate the radius and display it. This program is supposed to loop but all I'm getting is the "Thank you for using the software!" print function, it's not going through the loop at all and I can't seem to understand why.
#include <stdio.h>
int main(void){
float i,r, V;
for(i=0; i <= 4; i++){
while ( r != 0) {
printf("Enter a radius between 1 and 9: \n");
scanf ("%f", &r);
V= (3.141592)*r*r*10;
if (r>=1 && r<=9){
printf("The cylinder volume is %f\n", V);
}
else if (r > 9 || r < 0){
printf ("The input is out of the acceptable range. Select an integer less than $/n");
}
printf("Thank you for using the software!\n");
}
return 0;
}
You never initialize r before entering your while loop, so this is undefined behavior.
Additionally, you want to use int s for equality operators, i.e. == or !=. In your case, you might want to include an "error" that r can be within.
you have not initialize r before using it.
you must initialize it or use i to iterate though the loop.
INITIALIZE r here before using
for (i = 0; i <= 4; i++)
{
while (r != 0)
{
//code
}
}
To make sure the loop is executed at least once, you could use
do {
// your code
} while ( r != 0);
you better use a do while loop to do that they way you are trying to do.
for(i=0; i <= 4; i++){
do {
printf("Enter a radius between 1 and 9: \n");
scanf ("%f", &r);
V= (3.141592)*r*r*10;
if (r>=1 && r<=9){
printf("The cylinder volume is %f\n", V);
}
else if (r > 9 || r < 0){
printf ("The input is out of the acceptable range. Select an integer less than $/n");
}while ( r != 0)
printf("Thank you for using the software!\n");
}
i haven't tried this by my self.this might work.try it if it works.
Related
I've written a program which carries out matrix multiplication using functions. The function which i presume is wrong is as follows:
void obtainMatrixElems(int mtrx[][10], int row_elems, int col_elems){
printf("Kindly enter matrix elements: \n");
for(int x = 0; x < row_elems; x++){
for(int y = 0; y < col_elems; y++){
printf("Enter element at position %d,%d: \n", x+1, y+1);
scanf("&d", &mtrx[x][y]);
}
}
}
It seems you are missing a "+" at multAns[x][y] = matrix1[x][y] * matrix2[x][y];.
It should rather be:
// Also note the change variables used for referencing cells ...
multAns[x][y] += matrix1[x][z] * matrix2[z][y];
In case your last operation result is 0 then this explains why you get a 0 matrix..
EDIT:
The sign is one of the problems .. There is also something wrong with the way you get input from the user.
for(int x = 0; x < row_elems; x++){
for(int y = 0; y < col_elems; y++){
printf("Enter element at position %d,%d: \n", x+1, y+1);
// Note the change of "&" to "%" and the extra sequence
// "\r\n" which expects the user to press ENTER (i.e.:
// new line) between input cells
rc = scanf("%d\r\n", &mtrx[x][y]);
if (rc != 1) {
printf("ERROR: scanf did not proceed as expected\r\n");
}
}
}
There is an error in your call to scanf
It should read
scanf("%d", &mtrx[x][y]);
Also take care with hitting the Enter Key after each input. To be safe you should catch it. Use something like
scanf(" %d", &mtrx[x][x]);
I've just started to learn coding
#include <stdio.h>
int main(void)
{
int i=0, x=0;
for (i=1; i<=100; i++) {
x++;
if (x%5==0 || x%3==0)
printf("The numbers are : %d\n", &x);
}
return 0;
}
so I'm trying to print all the integers <=100 that are divisible by either 3 or 5.
In the line:
printf("the numbers are : %d", &x);
printf is expecting an integer because of the %d format specifier, you have given it the address of an integer, thats what the & means, address of x. To fix this give printf what it desires, an int:
printf("the numbers are : %d", x);
The ampersand (&) operator returns the address of a variable, which isn't what you want - you just want the value. Also, note that you don't need two variables (x and i) - you can just use the loop's counter:
printf("The numbers are:\n");
for (i = 1; i <= 100; i++) {
if (i % 5 == 0 || i % 3 == 0) {
printf("%d ", i);
}
}
Removing the & will eliminate the error. You may also want to move the "the numbers are:" statement outisde of the loop so the text does not print each time through the loop.
I have to write a do while loop that reads integers and computes their sum. It continues to add the integers until the user enters -1, but the -1 should not be added to the sum.
do
{
printf("Enter a number. Enter -1 to stop\n");
scanf(" %d", &n);
sum = sum + n;
printf("The sum is %d\n", sum);
}
while (n >= 0);
{
return 0;
printf("Have a nice day!\n");
}
Where am I going wrong? How do I get the -1 to avoid being added to the sum?
You need to declare n, i before use. And you need a break when detecting input -1.
int sum = 0, n = 0;
do
{
printf("Enter a number. Enter -1 to stop\n");
scanf("%d", &n);
if( -1 == n )
break; // <-- break here to get out the loop
sum = sum + n;
printf("The sum is %d\n", sum);
} while (1); // <-- note while(1) should be used instead of while(n >= 0), this makes sure your loop only exits when detecting input -1
Also there's no point adding a printf after you return - ie this code can be removed:
{
return 0;
printf("Have a nice day!\n"); // <-- any code after `return` is meaningless
}
There are a couple of ways to avoid having -1 added to the sum. Let's look at your code, first:
do
{
printf("Enter a number. Enter -1 to stop\n");
scanf(" %d", &n);
sum = sum + n;
printf("The sum is %d\n", sum);
}
while (n >= 0);
The do/while loop will exit if n is less than zero. This is one way to catch the -1 as a sentinel value, so no problem there.
Unfortunately, your code reads the number, adds it to the total, and then checks if it should stop. With that order, you always add before checking.
The most obvious, brute-force way to avoid adding the number is to add an explicit check:
do {
printf ... scanf ... ;
if (n != -1)
sum = sum + n;
} while (n >= 0);
Another way is to cheat. Given that -1 always comes at the end of the list, just undo that at the end:
do {
printf... scanf...;
sum = sum + n;
} while (n >= 0);
sum = sum - n; // CHEAT - just subtract out the last number
printf("The sum is %d\n", sum);
Finally, there is a trick you can use. Sometimes in these situations, you can "shift" the loop, so that you actually do things slightly out of order. In this case, you would like to test before you add. So put the add at the top of the loop, and just set things up so that the very first add is harmless:
n = 0; // X + 0 is X, so adding 0 is harmless.
do {
sum = sum + n;
printf ... scanf ...;
} while (n >= 0);
This may make you uncomfortable for a minute, but it's an idiom you'll see a lot. The loop code has been "twisted" inside the loop, so that the top of the "natural" loop does not start at the top of the actual loop body. It's a good trick to know.
The behavior of your do-while loop will be clearer if you think of it in terms of the GOTOs the compiler sees it as:
// prior code before here...
START_OF_DO:
printf("Enter a number. Enter -1 to stop\n");
scanf(" %d", &n);
sum = sum + n;
printf("The sum is %d\n", sum);
if ( n >= 0 ) {
goto START_OF_DO; // this is what "while (n >= 0);" does
}
return 0;
printf("Have a nice day!\n"); // <-- note that this will never print, because you have already returned
Formed this way, you can see that your sum = sum + n happens before you check if ( n >= 0).
(note I am not suggesting that you ever use goto! But sometimes it's helpful to see these things in terms of the machine instructions the compiler emits.)
You're overwriting the value of n before it checks the condition to run the loop again.
Make these changes and it should work now.
int input = 0; // Change
int n = 0; // Change
do
{
printf("Enter a number. Enter -1 to stop\n");
scanf(" %d", &input); // Change
if(input >=0 ) // Change
{
n = input; // Change
sum = sum + n;
printf("The sum is %d\n", sum);
}
}
while (input != -1); // Change
{
return 0;
printf("Have a nice day!\n");
}
Being at the very beginning of learning programming,
C in particular, I am struggling with how to restrict
printf only to the valid outputs, i.e. to make sure
that printf does not appear after the "break" statement.
I understand that my test for scanf is within the for
loop, and when i reaches 3 the for loop stops and printf
appears. I hope to find the correct way to both test
scanf, and make sure printf doesn't appear after
the break statement (in case user types in integers).
// a program that calculates the average of an array
of 3 floating-point values and check if correct inputs
are types in scans (only floating values)
#include <stdio.h>
#include <stdbool.h>
int main (void)
{
float values[3];
float element, average;
float sum = 0;
int i, n;
bool is_float;
printf ("Please, enter 3 floating values: \n");
for ( i = 0; i < 3; i++)
{
scanf ("%f", &element);
is_float = 1;
n = (int) element;
if (n - element == 0 || element == 0 )
{
is_float = 0;
printf ("Sorry, invalid input\n");
break;
}
else
{
values[i] = element;
sum += values[i];
}
}
printf ("The average of 3 values is %.2f\n", sum / 3);
return 0;
}
Thank you!
If you want the program to exit directly after this line:
printf ("Sorry, invalid input\n");
then you must replace the break statement directly below that printf line with something like this:
return 1;
On another note, when you want to print error messages, you should use fprintf() instead of printf(). The entire for loop would look like this:
for (i = 0; i < 3; i++)
{
scanf ("%f", &element);
is_float = 1;
n = (int) element;
if (n - element == 0 || element == 0 )
{
is_float = 0;
fprintf (stderr, "Sorry, invalid input\n");
return 1;
} else {
values[i] = element;
sum += values[i];
}
}
Is there a way to select multiple elements in array using one line of code in C? For instance, say I had the following code (assuming I already asked the user for twenty numbers, the first ten I asked to be positive and the last ten I asked to be negative):
if (myArray[0 through 9] > 0)
{
printf("Thank you for providing positive numbers!");
}
else
{
printf("Sorry, please try again!");
}
if (myArray[10 through 19] < 0)
{
printf("Thank you for providing negative numbers!");
}
else
{
printf("Sorry, please try again!");
}
What code could I substitute for "through"? I am fairly new to this language, and have never heard of a way of doing so. I know that with this particular code I could make two arrays, one for the positive numbers and one for the negative numbers, but I am curious to know for other programming projects.
Thank you for reading and answering!
There's nothing built-in that does it, you need to write a loop. Don't forget that array indexes start at 0.
int all_positive = 1;
int i;
for (i = 0; i < 10; i++) {
if (myArray[i] <= 0) {
all_positive = 0;
break;
}
}
if (all_positive) {
printf("Thank you for providing positive numbers!\n");
}
int a[20];
// entering values for the array
_Bool first_positive = 1;
for ( size_t i = 0; i < 10 && first_positive; i++ )
{
first_positive = 0 < a[i];
}
if ( first_positive ) puts( "Hura, first 10 elements are positive" );
_Bool last_negative = 1;
for ( size_t i = 10; i < 20 && last_negative; i++ )
{
last_negative = a[i] < 0;
}
if ( last_negative ) puts( "Hura, last 10 elements are negative" );
Instead of type name _Bool you can use type int if your compiler does not support _Bool
The program requests number of rows (1D array).
Then asks for 2 integers, whole numbers.
Then asks user to select 2 rows.
The sum of the 2 selected rows is then added.
#include <stdio.h>
int main ()
//1D_Array. Load Element and Add Sumline .
//KHO2016.no5. mingw (TDM-GCC-32) . c-ansi .
{
//declare
int a,b,c,d,e,sum1=0;
int array[50];
int i,j,elm1,elm2;
//valuate
printf ("Plot a number of elements [1 - 20]: ");
scanf ("%d",&a);
printf ("Plot a value : ");
scanf ("%d",&b);
printf ("Plot an increment value : ");
scanf ("%d",&c);
//calculate
{for (i<0;i<=a;i++)
{array[i] =(b+(++c)); // set value for variable [i], inside the array subscript. the vairable [i] must have an INT, and an increment to function !
sum1 = (sum1 + array[i]);
printf ("Row [%.2d] : %.2d + %.2d = %d\n",i,b,c,array[i]);}
printf ("\nSum total = %d\n",sum1);}
printf ("\nRow [%.2d] = %d\n",b,array[b]);
printf ("Row [%.2d] = %d\n",a,array[a]);
printf ("Select 2 Rows :\n");
scanf ("%d%d",&elm1,&elm2);
d=elm1;
e=elm2;
printf ("You selected Row [%.2d] = %d\n",d,array[d]);
printf ("You selected Row [%.2d] = %d\n",e,array[e]);
printf ("The sum of two selected Rows [%d]+[%d] : %d + %d = %d\n",d,e,array[d],array[e],array[d]+array[e]);
//terminate
return 0;
}