I want the user to input a height in range 2-8 inclusive if any other number is entered it should re-prompt user ... i have tried
int main(void)
{
int height;
do{
height = get_int("Enter The Height: ");
}
while (height<=1 && height<=9);
return height;
}
the program runs with no errors but even if I input 9 it does not re-prompt me..
You want to ask again as long as the height is outside of the desired range. So you want to ask again as long as the height is less than the minimum or greater than the maximum.
do {
...
} while (height < 2 || height > 8);
You were using && where you should have be using ||, and <= where you should have been using >=.
Your logic is incorrect. It should be while (!(2 <= height && height <= 8)).
For example when height == 9 in your original logic it ends up being false && true, which is false.
well you need the "OR" operator instead of "AND", to get in the loop you need one of the conditions should be different from the other ie., "< >".
#include <stdio.h>
int main(){
int i, j, k, h;
int a, b;
do
{
printf("Enter the heights: ");
scanf("%d", &h);
} while (h < 0 || h > 8);
for (i = 0; i <= h; i++)
{
for (k = h; k >= i; k--)
{
printf(" . ");
}
for (j = 0; j <= i; j++)
{
printf(" # ");
}
for (a = 0; a < 2; a++)
{
printf(" . ");
}
for (b = 0; b <= i; b++)
{
printf(" # ");
}
printf("\n");
}
}
Related
I've been trying to do this exercise for a while but i cant seem to find where the problem may lie.
So the exercise goes like this:
We are given a matrix with 1's and 0's with 1 representing islands and 0 representing water
We have to build a code that finds us
The highest(more 1 in y direction)
The widest (more 1 in x direction)
The island with the biggest area
We also have to print the initial coordinates where this island starts(the first 2 are the dimensions of the matrix. the reading is done from a file)
5 6
1 1 0 0 0 0
0 0 1 1 0 0
0 0 1 1 0 1
0 0 0 0 0 1
1 0 1 0 0 1
Here it should print:
Max height 3: coordinates(2,5)
Max width 2: coordinates(0,0) (in case there are many with same width we just print one of
them)
Max area 4:coordinates (1,2)
I have a basic idea on how to do it.
I tried to implement it but i cant seem to understand why its not working. For now im focusing on finding the max height and width. Im sorry in advance for the messy code(i'm a beginner so for now im more focused on solving the problem than making it look pretty)
This is what i have done so far
#include <stdio.h
#include <stdlib.h>
#include <ctype.h>
int main()
{
FILE *file;
file = fopen("file", "r");
int n, m;
int i, j;
fscanf(file, "%d %d", &n, &m);
int v[n][m];
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
fscanf(file, "%d", &v[i][j]);
}
}
printf("Original Matrix:\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
printf("%d ", v[i][j]);
}
printf("\n");
}
int maxheight = 0;
int height = 0;
int posx;
int posy;
i = 0;
j = 0;
while (i < n && j < m)
{
if (v[i][j] == 1)
{
height++;
i++;
}
else if (v[i][j] != 1)
{
if (maxheight <= height)
{
maxheight = height;
if (maxheight == height)
{
posx = i;
posy = j;
}
}
height = 0;
i++;
}
if (i == n)
{
i = 0;
j++;
height = 0;
}
}
printf("The biggest height =%d in position (%d ,%d)", maxheight, posx, posy);
int width = 0;
int maxwidth = 0;
int posx1;
int posy1;
i = 0;
j = 0;
while (i < n && j < m)
{
if (v[i][j] == 1)
{
width++;
j++;
}
else if (v[i][j] != 1)
{
if (maxwidth <= width)
{
maxwidth = width;
if (maxwidth == width)
{
posx1 = i;
posy1 = j;
}
}
width = 0;
j++;
}
if (j == m)
{
j = 0;
i++;
width = 0;
}
}
printf("\nThe biggest width =%d in position (%d ,%d)", maxwidth, posx1, posy1 - maxwidth);
return 0;
}
Think about the vertical max tests.
In the entire map was 1, codes reports the maxheight as 0.
Code needs to assess maxheight<=height after every height++;, not in the if(v[i][j]!=1) block.
Likewise for width.
I am able to crack it up till #symbol but not after that.
I am not aware of logic for implementing two pyramids side by side.
int main()
{
int i, space, n, j, l, k=0;
printf("Enter number excluding 1 and -ve numbers: ");
scanf("%d",&n);
for(i=1; i<=n-1; ++i, k=0)
{
for(j=0;j<(n/2);j++)
{
printf("\t");
}
for(space=1; space<=n-i; ++space)
{
printf(" ");
}
while(k != 2*i-1)
{
printf("* ");
++k;
}//End of upper star pyramid
printf("\n");
}
//logic for #
for(j=1;j<=(n-1);j++)
{
for(l=0;l<(n/2);l++)
{
printf("\t");
}
for(l=1; l<space; ++l)
{
printf(" ");
}
printf("#");
for(l=1; l<=n; l++)
{
printf(" ");
}
printf("#\n");
}
//after that I dont know how to work with two different pyramids side by side
return 0;
}
Here's an attempt...
Have used "#define"s for the characters to be printed.
Have utilized three do-while loops to break the task of printing the top pyramid, the "#"s and the bottom pyramid.
The solution would work for the examples provided or any "Odd" number, provided by the user.
For even numbers,the solution needs to be worked upon(either ways the top of the pyramid would not align at the center for even numbers).
Hope this is helpful.....feel free to play around with the code....
#include <stdio.h>
#define SPACE ' '
#define STAR '*'
#define AT '#'
int main(){
int num = 7;//input from the user, have used 7 as an example
int total_hor_length = num * 3;//# of characters to be printed on each line
int i = num;
int j = 0;
while(i > 0){//calculate the iterations
j++; //required for top and bottom
i -= 2; //Pyramids
}
int first_print = j; //# first loop iteration
int last_print = first_print; //# last loop iteration
int center_print = (num * 2 - 1)-(first_print + last_print);//# center loop
int pos = total_hor_length/2; //position of top of center pyramid
int count = 0;
int k;
i = 0;//re-initialize i for the first loop
//Logic for the Top Pyramid(first loop)
do{
for(k = 0;k < total_hor_length;++k){ //for input "n"
if((k < pos || k > pos+count)) //by the time this loop finishes
printf("%c",SPACE); //pos will be "n-1"
else //and count will be "n+1"
printf("%c",STAR);
}
putchar('\n');
pos--;
count+=2;
i++;
}while( first_print > i);
i = 0;//re-initialize i for the center loop
//Logic for the "#"s (second loop)
do{
for(k = 0;k < total_hor_length;++k){ //utilizing the values of "count"
if((k == count-1 || k == (count-1)+pos)) //and pos procured from the previous
printf("%c",AT); //loop to print the "#"s
else
printf("%c",SPACE);
}
putchar('\n');
i++;
}while(center_print > i);
i = 0;//re-initialize i for the center loop
//Logic for the Bottom Pyramids (Last loop)
do{
for(k = 0;k < total_hor_length;++k){
if(i == 0){
if((k <= pos-i) || (k > pos + num + i && k < total_hor_length - i))
printf("%c",STAR);
else if(k == pos+1||k == pos+num) //printing the line containing
printf("%c",AT); // asterisk and "#" symbols
else
printf("%c",SPACE);
}
else{
if((k >= i && k <= pos-i) || (k > pos + num + i && k < total_hor_length - i))
printf("%c",STAR); //printing the last lines
else
printf("%c",SPACE);
}
}
putchar('\n');
i++;
}while(i < last_print);
}
Can you help me find out why the second 0 in the array turns into 45 please.
Everything is okay but except this number makes the result goes wrong. I cannot find out what's the matter with this.
Here is my code:
#include <stdio.h>
int getuserchoice() {
int n;
printf("---ISBN Validate---");
printf("\n1-ISBN Checking");
printf("\n2-Quit");
printf("\nSelect: ");
scanf("%d", &n);
return n;
}
int main() {
long a[10];
long sum = 0;
int i = 0, n = 1;
long x;
if (getuserchoice() == 1) {
printf("\nEnter the values for ISBN number : ");
scanf("%ld", &x);
if (x > 0) {
while (x > 0) {
a[i] = x % 10;
x = x / 10;
i++;
}
}
for (i = 0; i < 10; i++)
printf("%ld\t", a[i]);
for (i = 0; i < 10; i++) {
sum += a[i] * n;
n++;
}
if (sum % 11 == 0)
printf("\nISBN Status: Valid!");
else
printf("\nISBN Status: Invalid!");
} else
printf("\nSee you later!");
getchar();
return 0;
}
By default uninitialized arrays contain garbage (literally anything). It so happens that that particular element contains 45 (amazing, isn't it?).
It stays 45 because leading 0s are discarded when reading a number (you should read it as a string (C++) or char[]), so you are never accessing that particular array element to give it a meaningful value.
Here's SO post on how to initialize an array with 0s in C.
I am trying to build a square/rectangle using "Xs" on a 1:1 scale with the length and width, but the logic seems to be not perfect
void draw (float x, float y) {
int i, j;
int length = (int)x + 0;
int width = (int)y + 0;
for (i = 1; i < length; i++) {
for (j = 1; j < width; j++) {
if (((i = 1) || (i = length)) && ((j = 1) || (j = width))) {
printf("x");
} else {
printf(" ");
}
}
printf("\n");
}
}
The problem is that the loop iterates endlessly printing x's everywhere. I'm expecting Xs to be printed out in either a square or rectangular shape (depending on the length or width).
I see 3 flaws in your logic.
you mix = (assignment) with == comparison.
This is why your loop never ends: you always reset i to 1 with your if (((i = 1) || (i = length)) &...
you're not going far enough with your variables:
if i < length, then you'll never have it reach length and print the bottom line of X's
you can't draw a rectangle because your && in the test (((i = 1) || (i = length)) && ((j = 1) || (j = width))) is too restrictive. It can't work if width different from length.
You must learn the logic by yourself using tutorials:
Here such questions will be discarded by sometime-sad people. But as a welcome, here it is (you can replace y in the second case by x, but I thought it would help you understand):
#include <stdio.h>
#include <string.h>
void draw (float x, float y)
{
int i,j;
int length = (int)x + 0;
int width = (int)y + 0;
for(i=1; i<=length; i++) {
for(j=1;j<=width;j++) {
if(((i==1)||(i==length))) {
printf("x");
} else {
if (((j==1)||(j==width))) {
printf("y");
} else {
printf(" ");
}
}
}
printf("\n");
}
}
So I have this array, I need to find the Min, Max, and Average. I have figured out how to get the Max, my code looks right for the minimum but it does not work so I must be missing something. I also need to find the average of the numbers entered. Everything I can find on this only works when you have the size of the array set. I have a max size set for the array but it does not get filled by user input. I have been staring and testing on this for days and still cant figure it out.
I know for the average I need something like (with added declartions at the top), but I cannot figure out how to get it to work:
for (i = 0; i < numOfGrades; i++) {
sum += grades[i];
}
avg = (double) sum / numOfGrades;
Here is the rest of my code:
#include <stdio.h>
int main(){
int grades [100];
int i = 0;
int small, big, input;
printf("Enter a series of grades. When done, enter any number above 100.\n\n");
while (i <= 100) { //while loop to set maximum for array
printf("Enter grade:");
if (scanf("%d", &input) == 1) {
if (input >= 0 && input <=100) {
grades[i] = input; //if good, add to array
i++;
}
else {
printf("\n\nExiting entry.\n");
printf("\n\nGrades entered:\n\n");
i = i - 1;
break; //exiting loop
}
}
}
int x, y;
for (x = 0; x <= i; x++) {
printf("Grade: %d\n", grades[x]); //print array
}
big = small = grades[0];
for (y = 0; y < i; y++) {
if (grades[y] > big) {
big = grades[y];
}
else if (grades[y] < small) {
small = grades[y];
}
}
printf("Highest number : %d\n", big);
printf("Smallest number: %d\n", small);
return 0;
}
Here you do the right thing:
for (x = 0; x <= i; x++) {
Because x <= i will take you from 0 to the value of i. Then in your min/max loop you do this:
for (y = 0; y < i; y++) {
So you are going from 0 to to i-1. You have 2 options:
Make the above loop correct with y <= i.
Dont decrement i in the while loop (Dont do this i = i - 1;) That way, i represents the number of numbers entered, instead of number of numbers entered -1 (you will need to fix the x <= i to x < i).
Here is a live example. http://ideone.com/ZEaDue
To calculate the average, you are doing the right thing, you can re-use i though:
int sum = 0, avg = 0;
for (y = 0; y < i; y++) {
sum += grades[y];
}
avg = (double) sum / i;
printf("Avg: %d\n", avg);
Actually you do have the number of grades
if (scanf("%d", &input) == 1) {
if (input >= 0 && input <=100) {
grades[i] = input; //if good, add to array
i++;
}
else {
numOfGrades=i;
printf("\n\nExiting entry.\n");
printf("\n\nGrades entered:\n\n");
i = i - 1;
break; //exiting loop
}
As for the minimum, your logic should be like this:
for (y = 0; y < numOfEdges; y++) {
if (grades[y] > big) {
big = grades[y];
}
if (grades[y] < small) {
small = grades[y];
}
}
The else statement I removed should do the trick. Also I would always use
for(int var = 0; var < numOfEdges; var++) in any of the loop constructs. It's easyer to follow the logic this way: You have counted the number of edges (numOfEdges), but your loop goes only to numOfEdges-1 since you start with index 0.