I have created a program which prints a square of specific dimensions and uses a particular character,based on user's input.My goal is to print multiple copies of this shape(the user will determine the number of copies each time),each next to each other.I tried using a for loop in main,but the copies are printed vertically.I also thought of printing the first line of the shape,the second one etc, but I don't know how to apply this to my code.
Any help?
#include <stdio.h>
int getsize(void);
char get_char(void);
void printsquare(int size,char ch);
int main(){
int size;
char ch;
size=getsize();
ch=get_char();
printsquare(size,ch);
}
int getsize(void){
int size;
printf("Enter the size of selected shape: ");
scanf("%d",&size);
return size ;
}
char get_char(void){
char character;
printf("Enter the character of selected shape: ");
scanf("\n%c",&character);
return character;
}
void printsquare(int size,char ch){
int i,j;
for (i=1;i<=size;i++){
for (j=1;j<i;j++){
printf("-");
}
if (i==1 || i==size){
for (j=1;j<=size;j++){
printf("%c",ch);
}
}
else{
printf("%c",ch);
for (j=1;j<=size-2;j++){
printf("-");
}
printf("%c",ch);
}
printf("\n");
}
return ;
}
remember always try think simple, the following code show how must printsquare must be
void printsquare(int size,char ch){
int i;
int j;
// print first line
for (i= 0; i < size; i++) {
putchar(ch);
}
putchar('\n');
// print inner line
for (i= 0; i < size; i++) {
putchar(ch);
for (j = 1; j < size - 1; j++) {
putchar('-');
}
putchar(ch);
putchar('\n');
}
// print last line
// print first line
for (i= 0; i < size; i++) {
putchar(ch);
}
putchar('\n');
}
another implementation
void printsquare(int size,char ch){
int i;
int j;
for (i= 0; i < size; i++) {
if (i == 0 || i == size - 1) {
// print last and first line
for (j = 0; j < size; j++) {
putchar(ch);
}
putchar('\n');
}
else {
// print inner lines
putchar(ch);
for (j = 1; j < size - 1; j++) {
putchar('-');
}
putchar(ch);
putchar('\n');
}
}
}
now for multiple copies
void printsquare(int size,char ch, int copies){
int i;
int j;
int c;
for (i= 0; i < size; i++) {
if (i == 0 || i == size - 1) {
for (c = 0; c < copies; c++) {
// print last and first line
for (j = 0; j < size; j++) {
putchar(ch);
}
putchar(' ');
}
putchar('\n');
}
else {
for (c = 0; c < copies; c++) {
// print inner lines
putchar(ch);
for (j = 1; j < size - 1; j++) {
putchar('-');
}
putchar(ch);
putchar(' ');
}
putchar('\n');
}
}
}
A simple solution is to repeat the entire printing logic of each line as many times as you need.
void printsquare(int size,char ch, int reps) {
int i,j;
for (i=1;i<=size;i++) {
for (int k = 0; k < reps; k++) {
for (j=1;j<i;j++){
printf("-");
}
if (i==1 || i==size){
for (j=1;j<=size;j++){
printf("%c",ch);
}
}
else{
printf("%c",ch);
for (j=1;j<=size-2;j++){
printf("-");
}
printf("%c",ch);
}
putchar(' ');
}
printf("\n");
}
return ;
}
I have added a space to differentiate the shapes, but it could be another dash, or nothing at all.
printsquare(4, '#', 2) gives the stdout:
#### ####
-#--# -#--#
--#--# --#--#
---#### ---####
I am making a program that sorts integers in ascending and descending order through bubble sort algorithm in C. So, I am providing a random data of 20 integers(fixed)first and then deciding in which manner to sort it, which is basically done through simple menu system which is like:
A. provide random data
B. Sort high to low
C. Sort low to high
*I want to print a message "Data not provided" if the user tries to sort without getting the random data first.
Code below:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void random_number_list(int array[]);
void low_to_high(int array[]);
void high_to_low(int array[]);
void display(int array[]);
int main(void)
{
int original_array[21];
char selection;
do
{
puts("----> Please make your selection from the following:\n\n"
" A.Define Random Number List\n"
" B.Sort Number List(High to Low)\n"
" C.Sort Number List(Low to High)\n"
" D.Exit ");
printf_s(" \nYour selection: ");
scanf_s("\n%c", &selection);
if (selection == 'A' || selection == 'a')
{
random_number_list(original_array);
}
else if (selection == 'B' || selection == 'b')
{
high_to_low(original_array);
}
else if (selection == 'C' || selection == 'c')
{
low_to_high(original_array);
}
else if (selection == 'D' || selection == 'd')
{
puts("\nThank you for using the application.\n");
return 0;
}
else
{
puts("\nSorry, input not understood. Please try again.\n");
}
}
while (selection != 'D');
}
void random_number_list(int array[])
{
srand(time(NULL));
printf("\n\nThe Random Data: ");
for (size_t i = 0; i < 20; i++)
{
array[i] = 1 + rand() % 100;
printf_s("%d,", array[i]);
}
puts("\n\n");
}
void low_to_high(int array[])
{
int i, j, temp;
for (i = 0;i < 20 - 1;i++)
{
for (j = i + 1;j < 20;j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
display(array);
printf("\n");
}
}
}
printf_s("\nSorted Data : ");
display(array);
puts("\n");
}
void high_to_low(int array[])
{
int i, j, temp;
for (i = 0; i < 20 - 1; i++)
{
for (j = i + 1; j < 20; j++)
{
if (array[i] < array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
display(array);
printf("\n");
}
}
}
printf_s("\nSorted Data : ");
display(array);
puts("\n");
}
void display(int array[])
{
for (size_t i = 0; i < 20; i++)
{
printf("%d,", array[i]);
}
}
What about using a boolean flag indicating whether option A was called? Initialize that flag with false and set it true in option A. Then in options B and C, test if the flag is true. If false, complain.
Showing us the code you have so far would be helpful.
I would include stdbool.h and create a boolean variable in the main function called something like is_generated set to false by default and set it to true when the option A is entered. Then you can check it instead of checking if original_array is equal to zero.
You can use the sizeof operator and divide the result by the size of an element.
size_t n = sizeof your_array
I'm trying to make a C program to insert elements into an array until user inputs a 0 or less number, as the title says. But when I print the array out, it doesn't show the numbers I inputted. I have tried using a while as well as do-while loops but without success.
#include <stdio.h>
int main() {
int data[100];
int i;
for (i = 0; i < 100; i++) {
printf("Input your number:\n");
scanf("%d", &data[i]);
if (data[i] <= 0) {
break;
}
}
printf("Your array:");
int n = sizeof(data[i]);
for (int i = 0; i < n; i++) {
printf("%d ", &data[i]);
}
}
Try this:
#include <stdio.h>
int main() {
int data[100];
int i;
int counter = 0;
for (i = 0; i < 100; i++) {
printf("Input your number:\n");
scanf("%d", &data[i]);
counter++;
if (data[i] <= 0) {
break;
}
}
printf("Your array:");
for (int j = 0; j < counter - 1; j++) {
printf("%d ", data[j]);
}
}
The problem was that you had printf("%d ", &data[i]); instead of printf("%d ", data[i]);.
And also you've trying to get the sizeof() of an element data[i], not the size of the whole array. That's why there's counter in my code.
int n = sizeof(data[i]);
this is wrong, you want
int n = i;
sizeof(data[i]) gives you the size of an int (4 on my machine)
On the other hand, you need to check the result of scanf, if a bad input is entered do not increment the counter, something like:
int i = 0;
while (i < 100)
{
int res = scanf("%d", &data[i]);
if (res == EOF)
{
break;
}
if (res == 1)
{
if (data[i] <= 0)
{
break;
}
i++;
}
else
{
// Sanitize stdin
int c;
while ((c = getchar()) != '\n');
}
}
Finally, scanf wants a pointer to the object, but this is not the case of printf:
printf("%d ", &data[i])
should be
printf("%d ", data[i])
I have the code for finding prime numbers within a range.
The problem is to remove the last comma.
#include<stdio.h>
int main()
{
int a,b,i,x,c,f=1;
scanf("%d%d",&a,&b);
for(x=a;x<=b;(x++,f=0))
{
for(i=2;i<x;i++)
{
if(x%i==0)
{
f=1;
}
}
if(f==0)
printf("%d,",x);
}
}
But the output contains an extra comma in the last.
For example
2,3,5,7,
whereas the expected output is
2,3,5,7
Instead of flag you can decide directly what you want to print between numbers
And note that you can break out of the internal loop as soon as f is set to 1
#include<stdio.h>
int main()
{
int a,b,i,x,c,f=1;
const char* delim = "";
scanf("%d%d",&a,&b);
for(x=a; x<=b; (x++,f=0))
{
for(i=2; i<x; i++)
{
if(x%i==0)
{
f=1;
break; //no need to continue the checking
}
}
if(f==0) {
printf("%s%d",delim,x);
delim = ", ";
}
}
putchar('\n');
}
#include<stdio.h>
int main()
{
int a,b,i,x,c,f=1;
char backspace = 8;
scanf("%d%d",&a,&b);
for(x=a;x<=b;(x++,f=0))
{
for(i=2;i<x;i++)
{
if(x%i==0)
{
f=1;
}
}
if(f==0)
printf("%d,",x);
}
printf("\b"); // or printf("%c", backspace);
}
Add another flag, just a simple counter that tells you if you are printing the first time then check the flag to decide what to print, e.g.
#include<stdio.h>
int main()
{
int a,b,i,x,c,first=0,f=1;
scanf("%d%d",&a,&b);
for(x=a;x<=b;(x++,f=0))
{
for(i=2;i<x;i++)
{
if(x%i==0)
{
f=1;
}
}
if(f==0)
{
if(first==0){
printf("%d",x);
}else{
printf(",%d",x);
}
first++
}
}
}
Use a flag to detect the first occurrence of printf() and print the first number as such without any ,. For consecutive number printing precede with ,
#include<stdio.h>
int main()
{
int a,b,i,x,c,f=1,flag=0;//Flag to mark first occurrence
scanf("%d%d",&a,&b);
for(x=a;x<=b;(x++,f=0))
{
for(i=2;i<x;i++)
{
if(x%i==0)
{
f=1;
break;// Once the condition fails can break of the for loop as it fails for the prime number condition at the first case itself
}
}
if(f==0)
{
if(flag==0)
{//Check if it is first time
printf("%d",x);
flag = 1;//If so print without ',' and set the flag
}
else
printf(",%d",x);// On next consecutive prints it prints using ','
}
}
}
This method also avoids the , when only one number is printed.
Eg: When input is 2 and 4. It prints just 3 and not 3,
Simply you need odd number best practice for minimum loop is given below;
#include<stdio.h>
int main()
{
int a,b,i,x,c,f=1;
scanf("%d%d",&a,&b);
while (a < b)
{
if ( (a%2) == 1) {
printf("%d", a);
if ( (a + 1) < b && (a + 2) < b)
printf(",");
}
a = a + 1;
}
}
please check from the site
http://rextester.com/MWNVE38245
Store the result into a buffer and when done print the buffer:
#include <stdio.h>
#include <errno.h>
#define RESULT_MAX (42)
size_t get_primes(int * result, size_t result_size, int a, int b)
{
int i, x, f = 1;
size_t result_index = 0;
if (NULL == result) || (0 == result_size) || ((size_t) -1 == result_size))
{
errno = EINVAL;
return (size_t) -1;
}
for (x = a; x <= b; (x++, f = 0))
{
for (i = 2; i < x; i++)
{
if (x % i == 0)
{
f = 1;
break;
}
}
if (f == 0)
{
result[result_index] = x;
++result_index;
if (result_size <= result_index)
{
fprintf(stderr, "Result buffer full. Aborting ...\n");
break;
}
}
}
return result_index;
}
int main(void)
{
int a = 0, b = 0;
int result[RESULT_MAX];
scanf("%d%d", &a, &b);
{
size_t result_index = get_primes(result, RESULT_MAX, a, b);
if ((size_t) -1 == result_index)
{
perror("get_primes() failed");
}
else if (0 == result_index)
{
fprintf(stderr, "No primes found.\n");
}
else
{
printf("%d", result[0]);
for (size_t i = 1; i < result_index; ++i)
{
printf(", %d", result[i]);
}
}
}
return 0;
}
This example uses a simple fixed-size buffer, if this does not suite your needs replace it by a dynamic one.
This is more of a "language-agnostic" problem: "How do I output a comma-separated list without a final comma?" It is not specifically about prime numbers.
You seem to be thinking of you list as a series of [prime comma] units. It isn't. A better way to think of it is as a single prime as the head of the list, followed by a tail of repeated [comma prime] units.
Some pseudocode to illustrate the general idea:
outputList(theList)
separator = ", "
output(theList.firstItem())
while (theList.hasMoreItems())
output(separator)
output(theList.nextItem())
endwhile
return
/* this is just logic */
for(i=2;i<=n;i++)
{
k=0;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
k=1;
}
if(k==0)
{
c++;
c++;
}
}
System.out.println(c);
for(i=2;i<=n;i++)
{
k=0;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
k=1;
}
if(k==0)
{
System.out.print(i);
b++;
if(b!=c-1)
{
System.out.print(",");
b++;
}
}
}
}
}
//comma separated values
#include <bits/stdc++.h>
using namespace std;
int Prime(int a, int n){
bool prime[n+1];
memset(prime,true,sizeof(prime));
for(int p=2;p*p<=n;p++){
if(prime[p]==true){
for(int i=p*p ; i<=n; i+=p ){
prime[i] = false;
}
}
}
for(int i = 2;i<= n;i++){
if(i==2) cout<<i; // here is the logic first print 2 then for other numbers first print the comma then the values
else if(prime[i]) cout<<","<<i;
}
}
int main(){
int a =2 ;
int n = 30;
Prime(a , n);
}
#include <stdio.h>
int main()
{
int i, j, n, count;
scanf("%d", &n);
for(i=2; i<n; i++)
{
count=0;
for(j=2; j<n; j++)
{
if(i%j==0)
count++;
}
if(count==1)
printf("%d," i);
}
printf("\b \b");
}
\b is a nondestructive backspace. It moves the cursor backward, but doesn't erase what's there, it replaces it. For a a destructive backspace,
use "\b \b" i.e. a backspace, a space, and another backspace.
This Program prints all the prime number up to given number with comma separated
My program takes 3 lines of input. The first line being whether you want to sort it by odd or even, the second line being how large your array is and the third line being the integers in the array. It works until you use an array larger than 8. I believe it's to do with malloc but I've tried to debug this code for a couple of hours now and I can't fix this issue.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* sort;
int n;
int* ar;
int i;
int test()
{
int temp;
int j = 1;
//printf("%s", sort);
if (strcmp(sort, "odd") == 0) {
for (i = 0; i < n;) {
if (j != n) {
if (ar[i] % 2 != 0) {
if (ar[j] % 2 != 0) {
if (ar[j] < ar[i]) {
temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
j++;
}
else {
j++;
}
}
else {
j++;
}
}
else {
j++;
i++;
}
}
else {
i++;
j = i + 1;
}
}
}
if (strcmp(sort, "even") == 0) {
for (i = 0; i < n; i++) {
if (j != n) {
if (ar[i] % 2 == 0) {
if (ar[j] % 2 == 0) {
if (ar[j] < ar[i]) {
temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
j++;
}
else {
j++;
}
}
else {
j++;
}
}
else {
j++;
i++;
}
}
else {
i++;
j = i + 1;
}
}
}
}
void main()
{
ar = malloc(sizeof(int) * n);
sort = malloc(sizeof(char) + 1);
printf("Enter odd or even\n");
scanf("%s", sort);
// printf("please input odd or even\n");
printf("Enter the size of the array \n");
scanf("%d", &n);
//printf("%s", sort);
printf("Enter the elements of the array \n");
for (i = 0; i < n; i++) {
scanf("%d", &ar[i]);
}
test();
for (i = 0; i < n; i++) {
printf("%d ", ar[i]);
}
// return 0;
}
Code is typically executed in a linear fashion, but you don't seem to be doing that. You're allocating ar using n, but don't have a value for n yet until several lines later...
ar = malloc(sizeof(int) * n);
sort = malloc(sizeof(char) + 1);
printf("Enter odd or even\n");
scanf("%s", sort);
// printf("please input odd or even\n");
printf("Enter the size of the array \n");
scanf("%d", &n);
You're also not allocating the size of sort big enough to contain any string longer than 1 character.