I created a finished code for a mario problem set.
Want to know if I understand the last if statement correctly.
If the user enters 4, the code will print out 4 columns with hashes, because it uses the remaining rows to print a blank space on each column.
Just wanted to know, if my point of view is correct.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int r, c, ans;
printf("Value between 0 and 9\n");
scanf("%d", &ans);
while (ans < 1 || ans > 8) {
printf("INVALID REPSONSE PLEASE TRY AGAIN\n");
scanf("%d", &ans);
}
if (ans > 0 || ans <= 8) {
for (r = 0; r <= ans; r++) {
for (c = 0; c < ans; c++) {
if (c >= ans - r) {
printf("#");
} else {
printf(" ");
}
}
printf("\n");
}
}
return 0;
}
Related
I want to write a program that reads an integer n from standard input, and prints an nxn pattern of asterisks and dashes in the shape of an "X".
N has to be odd and N>= 5.
my code:
#include <stdio.h>
int main(void) {
//making sure they enter an odd number >= 5
int endloop = 0;
int size;
while (endloop == 0) {
printf("Enter size:");
scanf("%d", &size);
if (size % 2 == 0 || size < 5) {
printf("Invalid, try again\n");
} else {
endloop = 1;
}
}
int downwards = 0;
while (downwards < size) {
int across = 0;
while (across < size) {
if (downwards == across) {
printf("*");
}
if (across == size - downwards - 1) {
printf("*");
} else {
printf("-");
}
across++;
}
downwards++;
printf("\n");
}
return 0;
}
result:
Enter size:5
*----*
-*--*-
--**--
-*-*--
*---*-
it's supposed to look like this:
*---*
-*-*-
--*--
-*-*-
*---*
i can't figure out what i did wrong. The across row is printing 6 characters instead of 5 even though i stated across < size
Our teacher gave us an exercise in C. We have to create a program that accepts three integers and outputs them if they are odd or even without using arrays, only loops and conditional statements can be used.
What I am only allowed to use are scanf(), printf(), loops and conditional statements. I must not have multiple variables like odd1, odd2, odd3, even1, even2, even3.And I must not do scanf("%d %d %d",), so I must run scanf("%d") three times in a loop.I couldn't think of any idea that would precisely print the same format of the expected output. I hope someone could help me on this
#include <stdio.h>
int main() {
int i, num;
printf("Enter three integers: ");
for(i=1;i<=3;i++)
{
scanf("%d", &num);
if(!(num%2))
{
printf("\nEven: %d", num);
}
if(num%2)
{
printf("\nOdd: %d\n", num);
}
}
}
I expect the following like this...
Input:
1 2 3
Output:
Odd: 1 3
Even: 2
Input:
2 4 6
Output:
Odd:
Even: 2 4 6
...but the only thing i can do is this
Input:
1 2 3
Output:
Odd: 1
Even: 2
Odd: 3
Input:
1 3 5
Output:
Odd: 1
Odd: 3
Odd: 5
Recursion (loop in disguise) for the win (if you don't mind having the even numbers reversed).
#include <stdio.h>
#include <stdlib.h>
void separate(int m, int n) {
if (m == n) printf("Odd:");
if (n == 0) { printf("\nEven:"); return; }
int i;
if (scanf("%d", &i) != 1) exit(EXIT_FAILURE);
// print odd numbers before recursing; even numbers after recursing
if (i % 2 == 1) printf(" %d", i);
separate(m, n - 1);
if (i % 2 == 0) printf(" %d", i);
if (m == n) printf("\n\n");
}
See https://ideone.com/GpE7rC which includes the calling and input
separate(3, 3); // for 3 numbers
One way would be say:
int odd1, odd2, odd3, numOdd = 0;
/* Code as above */
scanf("%d", &num);
if (num%2)
{
if (numOdd == 0)
{
odd1 = num;
numOdd++;
}
else if (numOdd == 1)
{
odd2 = num;
numOdd++;
}
/* And continue */
}
else
{
/* Repeat with even1, even2, even3 and numEven */
}
/* Print out numOdd oddn's */
/* Print out numEven evenn's */
It seems a silly exercise, but at least you'll get a lot of practise writing if statements...
I also thought:
scanf("%d", &num1);
scanf("%d", &num2);
scanf("%d", &num3);
Might be shorter, but then you don't need a for loop, unless you do something like:
printf("Odd: ");
for (i = 0; i < 3; i++)
{
int thisNum;
if (i == 0) thisNum = num1;
else if (i == 1) thisNum = num2;
/* etc */
if (thisNum % 2) printf("%d ", thisNum);
}
The problems is that you cannot use arrays. Hence you must not use strings, which are arrays. Allowing the use of scanf and printf is a deliberate trap set by your teacher - there is no way of using them in an useful way without resorting to use of arrays, explicit or implied.
Therefore you must do this:
#include <stdio.h>
#include <ctype.h>
void print_number(int n)
{
if (n < 0) {
putchar('-');
n = -n;
}
if (n == 0)
putchar('0');
if (n / 10)
print_number(n / 10);
putchar(n % 10 + '0');
}
int main(void) {
putchar('E');
putchar('n');
putchar('t');
putchar('e');
putchar('r');
putchar(' ');
putchar('t');
putchar('h');
putchar('r');
putchar('e');
putchar('e');
putchar(' ');
putchar('i');
putchar('n');
putchar('t');
putchar('e');
putchar('g');
putchar('e');
putchar('r');
putchar('s');
putchar(':');
putchar(' ');
fflush(stdout);
int odd1, odd2, odd3, odd_count = 0;
int even1, even2, even3, even_count = 0;
for(int i = 0; i < 3; i++)
{
int number = 0;
int c;
while (1) {
c = getchar();
if (isspace(c)) {
if (c == '\n')
break;
while ((c = getchar()) == ' ');
ungetc(c, stdin);
break;
}
else if (isdigit(c)) {
number = number * 10 + c - '0';
}
}
if (number % 2) {
switch (odd_count) {
case 0: odd1 = number; break;
case 1: odd2 = number; break;
case 2: odd3 = number; break;
}
odd_count ++;
}
else {
switch (even_count) {
case 0: even1 = number; break;
case 1: even2 = number; break;
case 2: even3 = number; break;
}
even_count ++;
}
}
putchar('O');
putchar('d');
putchar('d');
putchar(':');
putchar(' ');
if (odd_count >= 1) {
print_number(odd1);
}
if (odd_count >= 2) {
putchar(' ');
print_number(odd2);
}
if (odd_count >= 3) {
putchar(' ');
print_number(odd3);
}
putchar('\n');
putchar('E');
putchar('v');
putchar('e');
putchar('n');
putchar(':');
putchar(' ');
if (even_count >= 1) {
print_number(even1);
}
if (even_count >= 2) {
putchar(' ');
print_number(even2);
}
if (even_count >= 3) {
putchar(' ');
print_number(even3);
}
putchar('\n');
}
This code does not use any arrays.
If you cannot define any functions (other than main obviously), you must inline the print_number in the 6 places changing the recursion into iteration. Good luck!
If the teacher however accepts the use of strings in a task that does not allow the use of arrays, you can take it as a license to disregard all silly restrictions set by them.
Given the fact that you are learning C, perhaps your teacher wants to teach you some pointer arithmetics instead of using arrays like in this code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i = 0;
int *odd = (int*)malloc(3 * sizeof(int));
int odd_count = 0;
int *even = (int*)malloc(3 * sizeof(int));
int even_count = 0;
printf("Enter three integers: ");
for(i=0; i<3; i++)
{
int num = 0;
scanf("%d", &num);
if(!(num%2))
{
*even++ = num;
even_count++;
}
if(num%2)
{
*odd++ = num;
odd_count++;
}
}
even -= even_count;
odd -= odd_count;
printf("\nEven: ");
for(i=0; i<even_count; i++)
{
printf("%d ", *even++);
}
printf("\nOdd: ");
for(i=0; i<odd_count; i++)
{
printf("%d ", *odd++);
}
free(odd);
free(even);
return 0;
}
I need a program that asks for an integer &prints out an emtpy asterisk square based on the input number. And if the input was invalid then i need it to try and ask again
.
THIS is what i have so far (it already can make a square based on a variable number) i just need to enhence it so it asks again if the user is too dumb to input an integer
#include <stdio.h>
int Loops() {
int s, e, z;
printf("Input number \n");
scanf("%d", &s);
for(e = 0; e < s; e++) {
for(z = 0; z < s; z++) {
if(e==0 || e==s-1 || z==0 || z==s-1)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
int main() {
Loops();
}
i think the teacher mentioned something of adding a switch case...?
I propose this solution, in a few words I check the type of number entered by the user, if it's >0 then run,you do not have to worry that it is float because you have defined an integer type and the part not integer is automatically troncated.
#include <stdio.h>
int Loops() {
int s, e, z;
printf("Input number \n");
scanf("%d", &s);
if (s > 0 ) {
for (e = 0; e < s; e++) {
for (z = 0; z < s; z++) {
if(e == 0 || e == s - 1 || z == 0 || z == s - 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
else
Loops();
}
int main() {
Loops();
return 0;
}
You can simply use an infinite loop i.e. while(1) loop with a condition to break. Let's say you want a user to input positive integer and discard negative numbers and alphabets as well, you can do that in the following manner:
int Loops() {
int s, e, z;
while(1)
{
printf("Input number \n");
e = scanf("%d", &s);
if((s <= 0) || (e != 1))
{
printf("Please Enter Non-negaitve Number\n");
getchar(); //This is to consume the '\n'
}
else
{
break;
}
}
for(e = 0; e < s; e++) {
for(z = 0; z < s; z++) {
if(e==0 || e==s-1 || z==0 || z==s-1)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
I would like to get an output of the biggest even number. but when I input 1 2 3 (3 calls to scanf) the output is 4.
#include <stdio.h>
#include <stdlib.h>
int main() {
int ary[100];
int x, y = 0;
int amount;
scanf("%d", &amount);
fflush(stdin);
for (x = 1; x <= amount; x++) {
scanf("%d", &ary[x]);
if (ary[x] % 2 == 0) {
if (ary[0] < ary[x]) {
ary[0] = ary[x];
}
}
}
printf("%d", ary[0]);
getchar();
return 0;
}
Before the loop initialize ary[0] for example the following way (otherwise uninitialized value of ary[0] is used in the program)
ary[0] = 1;
then substitute these if statements
if(ary[x]%2==0)
{
if(ary[0]<ary[x])
for
if( ary[x]%2==0 && ( x == 1 || ary[0]<ary[x] ) )
And at last write
if ( ary[0] != 1 ) printf("%d",ary[0]);
Take into account that this call
fflush(stdin);
has undefined behavior and should be removed.
In fact there is no need to declare an array. Without the array the program can look like
#include <stdio.h>
int main( void )
{
unsigned int n;
int max_even = 1;
printf("How many numbers are you going to enter: ");
scanf("%u", &n);
int x;
for (unsigned int i = 0; i < n && scanf( "%d", &x ) == 1; i++)
{
if ((x % 2) == 0 && (max_even == 1 || max_even < x))
{
max_even = x;
}
}
if (max_even != 1)
{
printf("maximum entered even number is %d\n", max_even);
}
else
{
puts("None even number was enetered");
}
return 0;
}
Its output might look like
How many numbers are you going to enter: 10
0 1 2 3 4 5 6 7 8 9
maximum entered even number is 8
#include <stdio.h>
#include <stdlib.h>
int main() {
int ary[100];
int ary[0 = 0;
int x, y = 0;
int amount;
scanf("%d", &amount);
fflush(stdin);
for (x = 1; x <= amount; x++) {
scanf("%d", &ary[x]);
if (ary[x] % 2 == 0) {
if (ary[0] < ary[x]) {
ary[0] = ary[x];
}
}
}
printf("%d", ary[0]);
getchar();
return 0;
}
Your code does not work because ary[0] is not yet initialized the first time you compare its value to the value read, furthermore it might not be even for the other comparisons.
You should use an indicator telling you whether an even value has been seen.
Here is a solution:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int has_even = 0, max_even = 0, value, amount, x;
if (scanf("%d", &amount) != 1)
return 1;
for (x = 0; x < amount; x++) {
if (scanf("%d", &value) != 1)
break;
if (!has_even || value > max) {
max_even = value;
has_even = 1;
}
}
if (has_even)
printf("%d\n", max_even);
else
printf("no even value\n");
getchar();
return 0;
}
For my programming assignment I have to create 3 programs that print out an asterisk based triangle in c based on the user's input. The difference between the 3 programs would be one will use for loops, the other would use while loops and the last one would use goto. I have the for loop program as well as the goto program, but as for the while loop program I'm not sure how incorporate it into my program. This is my program with a for loop and the second program is my attempt at the while loop version.
#include <stdio.h>
int main() {
int lines, a, b;
//prompt user to input integer
do{
printf("Input a value from 1 to 15: ");
scanf("%d", &lines);
//Check if inputed value is valid
if(lines < 1 || lines > 15) {
printf("Error: Please Enter a Valid number!!!\n");
continue;
}
/*create triangle based on inputed value */
for(a = 1; a <= lines; a++) {
for(b=1; b<= a; b++) {
printf("*");
}
printf("\n");
}
} while(1);
system("pause");
}
Progam #2:
#include <stdio.h>
int main() {
int lines, a = 1, b = 1;
//prompt user to input integer
do{
printf("Input a value from 1 to 15: ");
scanf("%d", &lines);
//Check if inputed value is valid
if(lines < 1 || lines > 15) {
printf("Error: Please Enter a Valid number!!!\n");
continue;
}
while(a <= lines) {
a++;
while (b <= a) {
b++;
printf("*");
}
printf("\n");
}
} while(1);
system("pause");
}
Here is how you convert a for loop like the following
for (stat1; stat2; stat3) {
stat4;
}
to a while loop
stat1;
while (stat2) {
stat4;
stat3;
}
So here is the while loop you want:
a = 1;
while(a <= lines) {
b = 1;
while (b <= a) {
printf("*");
b++;
}
printf("\n");
a++;
}
Set b=1 before 2nd while loop
while(a <= lines) {
a++;
b=1; //you want to start b from 1 for each inner loop
while (b <= a) {
b++;
printf("*");
}
printf("\n");
}
The program2 can be changed as below. The below code result is equivalent to program1.`
#include <stdio.h>
int main() {
int lines, a = 1, b = 1;
//prompt user to input integer
do{
printf("Input a value from 1 to 15: ");
scanf("%d", &lines);
//Check if inputed value is valid
if(lines < 1 || lines > 15) {
printf("Error: Please Enter a Valid number!!!\n");
continue;
}
while(a <= lines) {
//a++;
while (b <= a) {
b++;
printf("*");
}
b =1;
a++1;
printf("\n");
}
} while(1);
system("pause");
}`