Iterating through code to add a new line on the nth term - c

How would I go about adding a new line on the 10th term on this zybooks question
#include <stdio.h>
int main(void) {
int n;
scanf("%d", &n);
printf("%d\t", n);
while (n > 1) {
if (n % 2 == 1) {
n = 3 * n + 1;
}
else {
n = n / 2;
}
printf("%d\t",n);
}
printf("\n");
return 0;
}

This solution uses a counter variable i which increments until 10 is reached. Once it is reached a newline character (\n) is printed.
#include <stdio.h>
int main(void)
{
int n, i = 0;
scanf("%d", &n);
printf("%d\t", n);
while (n > 1) {
i++;
(n % 2 == 1) ? (n = 3*n+1) : (n = n/2);
if (i == 10) {
i = 0;
printf("\n");
}
else {
printf("%d\t", n);
}
}
printf("\n");
return 0;
}

Related

User inputs a number, and program should surround it with charatchers

This is my code, it works but i feel i could do something different
#include <stdio.h>
int main()
{
int n;
printf("Input a number [0,9]: ");
scanf("%d", &n);
printf("*****\n");
printf("*****\n");
printf("**%d", n);
printf("**");
printf("\n*****\n");
printf("*****\n");
return 0;
}
Is this the best solution or is there something easier?
I tend to do 1 printf() per output line
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n;
printf("Input a number [0,9]: ");
if (scanf("%d", &n) != 1) exit(EXIT_FAILURE);
if ((n < 0) || (n > 9)) printf("nope\n");
else {
printf("*****\n");
printf("*****\n");
printf("**%d**\n", n);
printf("*****\n");
printf("*****\n");
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n;
printf("Input a number [0,9]: ");
if (scanf("%d", &n) != 1) exit(EXIT_FAILURE);
if ((n < 0) || (n > 9)) printf("nope\n");
else {
for (int i = 0; i < 5; i++) {
if (i == 2) {
printf("**%d**\n", n);
} else {
printf("*****\n");
}
}
}
return 0;
}
you can do it a bit more universal way. It will print NCHARS around the number and NLINES lines of FILLER around the number. Any number is covered (including negave ones)
#include <stdio.h>
#define NCHARS 3
#define NLINES 3
#define FILLER '*'
size_t countdigits(int n)
{
size_t ndigits = n <= 0 ? 1 : 0;
while(n)
{
ndigits++;
n /= 10;
}
return ndigits;
}
void printlines(size_t nchars, char ch)
{
for(size_t before = 0; before < NLINES; before++)
{
for(size_t pos = 0; pos < nchars; pos++ )
printf("%c", ch);
printf("\n");
}
}
void printfsurrounded(int n)
{
size_t nchars = countdigits(n);
printlines(nchars + NCHARS * 2, FILLER);
for(int pos = 0; pos < NCHARS; pos++) printf("%c", FILLER);
printf("%d", n);
for(int pos = 0; pos < NCHARS; pos++) printf("%c", FILLER);
printf("\n");
printlines(nchars + NCHARS * 2, FILLER);
}
int main(void)
{
printfsurrounded(4);
printf("\n");
printfsurrounded(1024);
printf("\n");
printfsurrounded(-233445);
}
https://godbolt.org/z/hz1eq9

why when I input any number between 14 and 20, the output is incorrect?

'I need to calculate and print an upside down pascal triangle, so I wrote down 2 functions for factorial and for the nCr, and I have followed, the equation x! / (y! * (x - y)!)'
#include <stdio.h>
#include <stdlib.h>
int Factorial (int value)
{
if (value == 1 || value == 0)
{
return 1;
}
return value*Factorial(value - 1);
}
int nCr(int value, int r)
{
return Factorial(value)/(Factorial(r) * Factorial(value - r));
}
int main(int argc, char **argv)
{
int value, i, j, k;
char* p;
value = strtol(argv[1], &p, 10);
if (*p != '\0')
{
return 1;
}
if (argc != 2)
{
return 1;
}
if (value < 1 || value > 20)
{
printf("Error: Please enter a value between 1 and 20 inclusively\n");
return 1;
}
else
'The problem is supposed to be in the nested loops I guess'
{
for (i = value - 1; i >= 0; i--)
{
for (j = value - i; j > 0; j--)
{
printf(" ");
}
for (k = 0; k <= i; k++)
{
printf("%d ", nCr(i, k));
}
printf("\n");
}
}
return 0;
}
The problem was in the declaration of the Factorial functions, as int type would overflow if the input is more than 13!, so we should declare the Factorial function as long, so that it does not overflow.

Malloc won't sort more than 8 inputs

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.

Need 10 outputs per line

I am having trouble refining some code. My code takes a number "n" and calculates that many prime numbers. I need to display 10 primes per line of output data. Any tips would be appreciated.
#include <stdio.h>
int main()
{
int n, i = 3, count, c;
printf("How many primes would you like?");
scanf("%d",&n);
if ( n >= 1 )
{
printf("2");
}
for ( count = 2 ; count <= n ; )
{
for ( c = 2 ; c <= i - 1 ; c++ )
{
if ( i%c == 0 )
break;
}
if ( c == i )
{
printf(" %d",i);
count++;
}
i++;
}
return 0;
}
Just try
printf(" %5d", i);
/* ^ to help align the numbers
and
if ((count + 1) % 10 == 0)
fputc(stdout, '\n');
fix for the first time when you already print 2.
bool is_prime(int anyNum) //takes an integer array returns, is_prime
{
bool is_prime = true;
for (int c = 2; c <= anyNum - 1; c++)
{
if (anyNum % c == 0)
{
//printf("%d is not prime\r\n" , anyNum);
is_prime = false;
}
}
return is_prime;
}
int main()
{
int num_primes;
printf("How many primes would you like: ");
std::cin >> num_primes;
printf("\r\nScanned Primes Are---\r\n");
int foundPrimes = 0;
int x = 0;
for (; x <= num_primes; x++)
{
bool gotLuckyFindingPrime = is_prime( x );
if (gotLuckyFindingPrime)
{
if (foundPrimes % 10 == 0)
{
printf("\r\n");
}
printf(" %d", x);
foundPrimes = (foundPrimes + 1) % 10;
}
}
}
Does handle ten digit showing on cmd too, you can experiment with formatting

SPOJ shows wrong answer to the my solution to: http://www.spoj.com/BSCPROG/problems/SMPCPH1/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
main() {
int n, m, i, j, k;
char a[100], b[100];
scanf("%d", &n);
if (n > 26 && n <= 1)
exit(0);
scanf("%s", a);
scanf("%d", &m);
if (m > 100 && m <= 1)
exit(0);
for (i = 0; i < m; i++) {
fgets(b, 100, stdin);
for (j = 0; b[j] != '\0'; j++) {
for (k = 0; k < n; k++) {
if (a[k] == b[j]) {
if (k == (n - 1)) {
b[j] = a[0];
break;
} else {
b[j] = a[k+1];
break;
}
}
}
}
printf("%s",b);
}
return 0;
}
I just have written C code for the problem: http://www.spoj.com/BSCPROG/problems/SMPCPH1/
With the given example my program gives desired result but spoj says it is wrong. How can I find mistakes?
You need 3 changes in your code:
Use || instead of && in your argument validation tests.
Scan and ignore the line feed after the number parsed into m, otherwise the next fgets() will read an empty line
Here is the corrected code:
if (n > 26 || n <= 1)
exit(0);
scanf("%s", a);
scanf("%d%*c", &m);
if (m > 100 || m <= 1)
exit(0);

Categories

Resources