putting a number in a hollow shape - c

I want to put number that can be one,two,three,four and so forth digits in the shape. Nonetheless, extra spaces is shifted. Besides, center is shifted. I trying different ways,however,i can't. How can i fix the problem. Thank you for all appreciated answer.
output example is:
http://i.stack.imgur.com/Kozr1.png
#include <stdio.h>
/* Function Prototypes */
int countNumber(int number);
int main() {
int n; /* number to put in the center of the shape */
int column, row; /* take values of column and row via user */
int columnCount, rowCount; /* count number of column and row */
int i; /* count how many spaces remained */
printf("Enter column:\n>");
scanf("%d",&column);
printf("\nEnter row:\n>");
scanf("%d",&row);
printf("\nEnter number for center:\n>");
scanf("%d",&n);
if((10<column && column<40) && (10<row && row<40)){
for (columnCount = 0; columnCount < column; columnCount++) {
for (rowCount = 0; rowCount < row; rowCount++) {
/* Middle Row */
if (columnCount == column/2) {
printf("#");
for (i = 0; i < ((row*2-3)-countNumber(n))/2;i++)
printf(" ");
printf("%d", n);
for (i = 0; i < ((row*2-3)-countNumber(n))/2;i++)
printf(" ");
printf("#");
break;
}
if (columnCount==0 || columnCount==column-1 || rowCount==0 || rowCount==row-1)
printf("# ");
else
printf(" ");
}
printf("\n");
}
}
else
printf("Please enter value btw 10-40");
return 0;
}
/* Function */
int countNumber(int number) {
int count;
for(count=0;0<number;count++) /* the number how many decimal places have */
number/=10;
return count;
}

this one works with any number
#include <stdio.h>
int digits(int n)
{
int i;
for(i=0;n;i++,n=n/10);
return i;
}
int main()
{
int i,j;
int column,row;
int n,cnt;
printf("Enter column: ");
scanf("%d",&column);
printf("Enter row: ");
scanf("%d",&row);
scanf("%d",&n);
cnt=digits(n);
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
if(i==0 || i==(row-1) || j==0 || j==(column-1))
printf("*");
else if((i==(row/2)) && (j==((column-cnt)/2)))
{
printf("%d",n);
j=j+(cnt-1);
}
else
printf(" ");
}
printf("\n");
}
return 0;
}

This should work!
It still has a small margin of error but I try to solve it
/*Import*/
#include <stdio.h>
/*Function Prototyp*/
int countNumber(int number);
int main() {
/*Variable*/
int number, column, row;
int columnCount, rowCount;
int i;
printf("Enter column:\n>");
scanf("%d",&column);
printf("\nEnter row:\n>");
scanf("%d",&row);
printf("\nEnter number for center:\n>");
scanf("%d",&number);
for (columnCount = 0; columnCount < column; columnCount++) {
for (rowCount = 0; rowCount < row; rowCount++) {
//Middle Row
if (columnCount == column/2) {
printf("*");
for (i = 0; i < ((row*2-3)-countNumber(number))/2;i++)
printf(" ");
printf("%d", number);
for (i = 0; i < ((row*2-3)-countNumber(number))/2;i++)
printf(" ");
if (countNumber(number % 2 == 0))
printf(" *");
else
printf("*");
break;
}
if (columnCount==0 || columnCount==column-1 || rowCount==0 || rowCount==row-1)
printf("* ");
else
printf(" ");
}
printf("\n");
}
return 0;
}
int countNumber(int number) {
int count;
for (count = 0; number > 0; count++)
number = number / 10;
return count;
}

Related

Repeat the Program for again Search Array Element

Repeat the Program for again Search Array Element.
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE];
int size, i, toSearch, found;
char repeat;
printf("Enter the size of an array\n");
scanf("%d", &size);
printf("Enter the array elements\n");
for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
do{
printf("\nEnter element to search: ");
scanf("%d", &toSearch);
found = 0;
for(i=0; i<size; i++)
{
if(arr[i] == toSearch)
{
found = 1;
break;
}
}
if(found == 1)
{
printf("\n%d is found at position %d", toSearch, i + 1);
}
else
{
printf("\n%d is not found in the array \n", toSearch);
}
printf("\n \n \nPress Y to again Search Any Element in Array\n \nPress Any other Key to Exit the Program\n\n");
scanf(" %c \t",&repeat);
}
while(repeat == 'y' || repeat == 'Y' );
return 0;
}
I want to repeat my program when user give the input of Y || y otherwise it'll exit the program.
In this code i want to make an array then search the element after this show's the results and in last repeat the code from the search the element block.
I ran your code and everything seems to be working properly except for this line:
scanf(" %c \t",&repeat);
Remove the \t from the scanf and it should work properly. You don't want to scan for a tab character, just the 'Y' or 'y' character.
Also, your use of newlines is a bit unusual. Try putting newline characters at the end of your strings as opposed to the beginning.
Updated code:
#include <stdio.h>
#define MAX_SIZE 100
int main() {
int arr[MAX_SIZE];
int size, i, toSearch, found;
char repeat = ' ';
printf("Enter the size of an array\n");
scanf("%d", &size);
printf("Enter the array elements\n");
for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
do{
printf("Enter element to search: \n");
scanf("%d", &toSearch);
found = 0;
for(i=0; i<size; i++) {
if(arr[i] == toSearch) {
found = 1;
break;
}
}
if(found == 1)
printf("%d is found at position %d\n", toSearch, i + 1);
else printf("%d is not found in the array\n", toSearch);
printf("Press Y to again Search Any Element in Array\nPress Any other Key to Exit the Program\n");
scanf(" %c",&repeat);
}
while(repeat == 'y' || repeat == 'Y' );
return 0;
}
Enclose the block of code you want to repeat in a while loop, something like
bool flag = false;
while(flag==true) {
//Code block
scanf("%c",&input)
if((input == 'y') || (input == 'Y')) {flag = true;}
else {flag = false;}
}
The first method that came to my mind :
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE];
int size, i, toSearch, found;
char repeat;
printf("Enter the size of an array\n");
scanf("%d", &size);
printf("Enter the array elements\n");
for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
do
{
printf("\nEnter element to search: ");
scanf("%d", &toSearch);
found = 0;
for (i = 0; i < size; i++)
{
if (arr[i] == toSearch)
{
found = 1;
break;
}
}
if (found == 1)
{
printf("\n%d is found at position %d", toSearch, i + 1);
}
else
{
printf("\n%d is not found in the array \n", toSearch);
}
printf("\n \n \nPress Y to again Search Any Element in Array\n \nPress Any other Key to Exit the Program\n\n");
repeat = getchar();
repeat = getchar();
if(repeat == 'y' || repeat == 'Y') {
continue;
}
else {
break;
}
} while (1);
return 0;
}

Bug Histogram Display in C

i want to do a histogram vertical display of my "election".
My code work just it seems to display wrongly the vote for the 10,11,12,13 value because i think there is 2 numbers ...
thank you for your time ;)
int main() {
char character ;
int TAB[12] = {0} ;
int vote = 0;
int I;
printf("Please enter a character:\n"); //character for the display
scanf(" %c", &character); // character we want to display in the histogram
printf("Please enter votes\n");
while(1) {
scanf("%d", &vote);
if (vote == -1) {
break;
}
TAB[vote-1]++; //save the vote into the array
}
printf("Histogram :\n");
/* Search for the maximum value */
int MAX=0;
for (I=0; I<12; I++)
{
if(TAB[I]>TAB[MAX]) MAX=I;
}
int maximum = TAB[MAX]; // maximum value
while (maximum > 0) {
for (I = 0; I < 12; I++) {
if (TAB[I] == maximum) {
printf("%c ",character);
TAB[I] = (TAB[I] - 1) ;
}
else {
printf(" ");
}
}
maximum= maximum - 1;
printf("\n");
}
for (I = 0; I < 13; I++) {
printf("%d ",I+1); // display the number of each candidat
}
printf("\n"); // go to the line
return 0;
}
You shouldn't use magic number 12, use like #define VOTE_MAX 13
For the range of vote, use if (vote <= 0 || vote > VOTE_MAX)
Format output, like printf("%2c ", character);
The line for (I = 0; I < 13; I++) { should be for (I = 0; I < 12; I++) {
The following code could work:
#include <stdio.h>
#define VOTE_MAX 13
int main() {
char character;
int TAB[VOTE_MAX] = {0};
int vote = 0;
printf("Please enter a character:\n"); // character for the display
scanf(" %c", &character); // character we want to display in the histogram
printf("Please enter votes\n");
while (1) {
scanf("%d", &vote);
if (vote <= 0 || vote > VOTE_MAX) {
break;
}
TAB[vote - 1]++; // save the vote into the array
}
printf("Histogram :\n");
/* Search for the maximum value */
int MAX = 0;
for (int i = 0; i < VOTE_MAX; ++i) {
if (TAB[i] > TAB[MAX]) MAX = i;
}
int maximum = TAB[MAX]; // maximum value
while (maximum > 0) {
for (int i = 0; i < VOTE_MAX; ++i) {
if (TAB[i] == maximum) {
printf("%2c ", character);
--TAB[i];
} else {
printf("%2c ", ' ');
}
}
--maximum;
printf("\n");
}
for (int i = 0; i < VOTE_MAX; ++i) {
printf("%2d ", i + 1); // display the number of each candidat
}
printf("\n"); // go to the line
return 0;
}
You need to add an extra space for the cases where I is 10 or higher.
Like:
for (I = 0; I < 12; I++) {
if (TAB[I] == maximum) {
printf("%c ",character);
TAB[I] = (TAB[I] - 1) ;
}
else {
printf(" ");
}
if (I >= 9) printf(" "); // Add extra space for 10, 11 and 12
}
BTW: your input method should be improved. Currently the user can enter values that will make your write outside the array. At least do something like:
while(1) {
if (scanf("%d", &vote) != 1) break; // Catch illegal input
if (vote <= 0 || vote >= 13) { // Only allow vote to be 1, 2, …, 10, 11, 12
break;
}
TAB[vote-1]++; //save the vote into the array
}

no repeating values in array

I need to make a program that stores numbers inside of an array. But it must have no duplicate elements.
int x;
int z[8];
for( x = 0; x<8;x++)
printf("number: ");
scanf("%d",&z[x]);
}
for( x=0;x<8;x++) {
printf("%d ",z[x]);
}
First, initialize the array, so that you do not end up reading an uninitialized value and fail the test.
int user_nums[6] = {0};
Next, you need to have another check in the for loop, to read the number again if it is a duplicate.
The code will look like this.
#include<stdio.h>
int main(){
int x,y;
int exists = 0;
int user_nums[6] = {0};
for( x = 0; x<6;x++){//for loop to get the players selected numbers
do {
exists = 0;
printf("Enter a number(from the #'s 1-42): ");
scanf("%d",&user_nums[x]);
for(y =0; y < x; y++) { //to check for duplicates
if (user_nums[x] == user_nums[y])
{
printf("Number already exists\n ");
exists = 1;
break;
}
}
}while (user_nums[x]<1 || user_nums[x]>42 || exists);//accepts only numbers from 1-42 which are not duplicates (continous to ask you for a number until condition is met).
}
printf("Your numbers: \n");
for( x=0;x<6;x++){
printf("%d ",user_nums[x]); // prints the numbers you inputed.
}
return 0;
}
The following code could work in O(n):
#include<stdio.h>
int main()
{
int user_nums[6];
int index[50];
for (int i = 0; i != sizeof(index) / sizeof(index[0]); ++i)
index[i] = -1;
for (int i = 0; i < sizeof(user_nums) / sizeof(user_nums[0]); ++i) {
for (;;) {
printf("Enter a number(from the #'s 1-42): ");
scanf("%d", user_nums + i);
if (user_nums[i] < 1 || user_nums[i] > 42) {
printf("wrong number\n");
continue;
}
if (index[user_nums[i]] != -1) {
printf("dump number\n");
continue;
}
index[user_nums[i]] = i;
break;
}
}
printf("Your numbers: \n");
for(int i = 0; i < 6; ++i)
printf("%d ", user_nums[i]);
return 0;
}

C Program triangle

I need to prompt a user to enter a number 'n' then print print stars in separate lines, for exanple if the user enters 5 the following should be printed (Using a while loop)
*
**
***
****
*****
****
***
**
*
The code i have doesn't output that
int n, x = 1, y = 1;
printf("enter a number : ");
scanf_s("%d", &n);
while (x <= n){
x++;
while (y <= x){
while (y >= n){
y--;
printf("*");
}
printf("\n");
}
system("pause");
}
}
This should work for you:
#include <stdio.h>
int main() {
int number, count, characterCount;
printf("Please enter a number:\n>");
scanf("%d", &number);
for(count = 1; count <= number; count++) {
for(characterCount = 1; characterCount <= count; characterCount++)
printf("*");
printf("\n");
}
for(count = 1; count < number; count++) {
for(characterCount = number-1; characterCount >= count; characterCount--)
printf("*");
printf("\n");
}
return 0;
}
Edit after comment:
Solution with while loop:
#include <stdio.h>
int main() {
int number, count = 1, characterCount;
printf("Please enter a number:\n>");
scanf("%d", &number);
while(count <= number) {
characterCount = 1;
while(characterCount <= count) {
printf("*");
characterCount++;
}
printf("\n");
count++;
}
count = 1;
while(count < number) {
characterCount = number-1;
while( characterCount >= count) {
printf("*");
characterCount--;
}
printf("\n");
count++;
}
return 0;
}
You can make it using only 2 for loops like this:
int i,k,n,flag=0;
printf("Enter Number of Rows :");
scanf("%d",&n);
for(i=-n;i<=n;i++)
{
if(i < 0)
{
i = -i;
flag=1;
}
for(k=(n-i)+1;k>0;k--)
{
printf("*");
}
if(flag==1)
{
i=-i;flag=0;
}
printf("\n");
}
For example if user enter value 2 it will print
*
**
***
**
*

Replacing elements in an array

I have a problem thats giving me a huge ache.
This piece of code purpose is to fill up an array with integer values and at the same time defend against strings and etc....but it doesn't defend against duplicates, but tried I got to far as replacing the number with a new number for example
Enter 6 integers
1, 2, 2, 3, 4, 5
my code will let me replace that 2 at position 1 with another number. What I want it to do is not to repeat the same number again, for example please replace 2 at position 1. I dont want the user to enter 2 again... and I want to make it to double check the work the array if any repeating numbers exists thank you.
system("clear");
printf("\nEntering Winning Tickets....\n");
nanosleep((struct timespec[]){{1, 0}}, NULL);
system("clear");
char userInput[256];
char c;
int duplicationArray[6] = {-1, -1, -1, -1, -1, -1};
for (i = 0; i < 6; i++)
{
printf("\nPlease enter the %d winning ticket number!(#'s must be between 1-49): ", i+1);
fgets(userInput, 256, stdin);
if ((sscanf(userInput, "%d %c", &winningNumbers[i], &c) != 1 || (winningNumbers[i] <= 0) || winningNumbers[i] >= 50))
{
printf("\nInvalid Input.\n") ;
nanosleep((struct timespec[]){{0, 350000000}}, NULL);
system("clear");
i = i - 1;
}
}
for (i = 0; i < 6 - 1; ++i)
{
min = i;
for (j = i+1; j < 6; ++j)
{
if (winningNumbers[j] < winningNumbers[min])
min = j;
}
temp = winningNumbers[i];
winningNumbers[i] = winningNumbers[min];
winningNumbers[min] = temp;
}
for (i = 0; i < 6; i++)
{
if (winningNumbers[i] == winningNumbers[i+1])
{
duplicationArray[i] = i;
duplicationCounter++;
}
else
{
duplicationCounter--;
}
}
if (duplicationCounter > -6)
{
for (i = 0; i < 6; i++)
{
int j, min, temp;
min = i;
for (j = i+1; j < 6; ++j)
{
if (duplicationArray[j] > duplicationArray[min])
min = j;
}
temp = duplicationArray[i];
duplicationArray[i] = duplicationArray[min];
duplicationArray[min] = temp;
}
for (i = 0; i < 6; i++)
{
if (duplicationArray[i] == -1)
{
zeroCounter++;
}
}
int resize = (6 - zeroCounter)+1;
for (i = 0; i <= resize; i++)
{
if (duplicationArray[i] == -1)
{
i++;
}
else if (duplicationArray[i] != -1)
{
system("clear");
printf("\nDuplicated numbers has been dected in your array. ");
printf("\nPlease replace the number %d at postion %d with another number: ", winningNumbers[duplicationArray[i]], duplicationArray[i]);
fgets(userInput, 256, stdin);
if ((sscanf(userInput, "%d %c", &winningNumbers[duplicationArray[i]], &c) != 1 || (winningNumbers[i] <= 0) || winningNumbers[i] >= 50))
{
printf("\nInvalid Input.\n") ;
nanosleep((struct timespec[]){{0, 350000000}}, NULL);
system("clear");
i = i - 1;
}
}
}
duplicationCounter = 0;
for (i = 0; i < 6; i++)
{
if (winningNumbers[i] == winningNumbers[i+1])
{
duplicationArray[i] = i;
duplicationCounter++;
}
else
{
duplicationCounter--;
}
}
printf("%d, ", duplicationCounter);
}
#include <stdio.h>
#include <stdint.h>
#define DATA_SIZE 6
int main(void){
char userInput[256];
int inputNum, winningNumbers[DATA_SIZE];
uint64_t table = 0;
int i=0;
while(i<DATA_SIZE){
printf("\nPlease enter the %d winning ticket number!(#'s must be between 1-49): ", i+1);
fgets(userInput, sizeof(userInput), stdin);
if(sscanf(userInput, "%d", &inputNum) != 1 || inputNum <= 0 || inputNum >= 50)
continue;
uint64_t bit = 1 << inputNum;
if(table & bit)
continue;
table |= bit;
winningNumbers[i++] = inputNum;
}
for(i=0;i<DATA_SIZE;++i)
printf("%d ", winningNumbers[i]);
printf("\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define DATA_SIZE 6
int inputNumberWithRangeCheck(const char *msg, const char *errMsg, int rangeStart, int rangeEnd){
char inputLine[256];
int n;
for(;;){
printf("%s", msg);
fgets(inputLine, sizeof(inputLine), stdin);
if(sscanf(inputLine, "%d", &n) != 1 || n < rangeStart || n > rangeEnd)
fprintf(stderr, "%s", errMsg);
else
return n;
}
}
int inputNumber(void){
return inputNumberWithRangeCheck(
"\nPlease enter the winning ticket number!(#'s must be between 1-49): ",
"Invalid Input.\n",
1,49);
}
int *inputArray(int *array, size_t size){
int i;
for(i=0;i<size;++i){
printf("\nInput for No.%d\n", i+1);
array[i] = inputNumber();
}
return array;
}
int **duplicateCheck(int *array, size_t size){
int **check, count;
int i, j;
check = malloc(size*sizeof(int*));
if(!check){
perror("memory allocate\n");
exit(-1);
}
//There is no need to sort the case of a small amount of data
//(Cost of this loop because about bubble sort)
for(count=i=0;i<size -1;++i){
for(j=i+1;j<size;++j){
if(array[i] == array[j]){
check[count++] = &array[i];
break;
}
}
}
check[count] = NULL;
if(count)
return check;
else {
free(check);
return NULL;
}
}
int main(void){
int winningNumbers[DATA_SIZE];
int **duplication;
int i, j;
inputArray(winningNumbers, DATA_SIZE);
while(NULL!=(duplication = duplicateCheck(winningNumbers, DATA_SIZE))){
for(i=0;i<DATA_SIZE;++i){
if(duplication[i]){
printf("\nyour input numbers : ");
for(j=0;j<DATA_SIZE;++j)
printf("%d ", winningNumbers[j]);
fprintf(stderr, "\nThere is duplicate. Please re-enter.\n");
*duplication[i] = inputNumber();
} else
break;
}
free(duplication);
}
for(i=0;i<DATA_SIZE;++i)
printf("%d ", winningNumbers[i]);
printf("\n");
return 0;
}

Resources