I'm a beginner C programmer and a college freshman.
I need a little help here with a test i'm working on here.
I want to make a nested loop that shows a sorted number. Sorta like this:
1
3 2
4 5 6
10 9 8 7
11 12 13 14 15
21 20 19 18 17 16
22 23 24 25 26 27 28
... ... ... and so on, depending the limit of rows you input
I already tried to make a crude trial-and-error test code:
int i;
int j;
int limit;
int number1 = 1;
int number2 = 3;
int spesial = 0;
printf("Input limit : ");
scanf("%d", &limit);
for (i=1;i<=limit;i++)
{
for(j=1;j<=i;j++)
{
if (i%2==0)
{
printf("%d ", number2);
number2--;
}
else
{
printf("%d ", number1);
}
number1++;
}
if (i%2==0)
{
number2=(i*6)-i+(spesial*1);
spesial+=1;
}
printf("\n");
}
I managed to make it sorted to the 7th rows, but the rest are not..
help please...
I want to know if we could actually control the position of the output without sorta crude our way like this.
Also, sorry for my English... I'm not really from an English speaking country and this is my first time posting/question in this site.
Thank you for reading this lengthy question and I hope you have a good day and good night.
https://ideone.com/yCxpHo:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int rows;
int i, j;
int n = 0;
printf ("How many rows do you want? ");
if (scanf("%d", & rows) != 1 || rows < 1) return EXIT_FAILURE;
printf ("\n");
for (i = 1; i <= rows; ++ i) {
for (j = 0; j < i; ++ j) {
printf ("%4d", n + (i % 2 == 0 ? i - j : j + 1));
}
printf ("\n");
n = n + i;
}
return EXIT_SUCCESS;
}
It can be more convenient to create another function that will calculate the biggest number of a row (I called it lineMax).
int lineMax(int num){
int cnt=0;
for (int i=1;i<=num;i++)
cnt+=i;
return cnt;
}
void main(){
int i,j,limit;
printf("Input limit : ");
scanf("%d", &limit);
for(i=1;i<=limit;i++){
if(i%2==0){ //right to left
for(j=lineMax(i);j>=lineMax(i-1)+1;j--)
printf("%d ",j);
}
else{ //left to right
for(j=lineMax(i-1)+1;j<=lineMax(i);j++)
printf("%d ",j);
}
printf("\n");
}
}
You are making a lot of special cases with number1, number2 and special. This will not work for bigger numbers.
One way is to calculate count which will give you the value to start from in each loop of j. count += i and then every time print count -j
count = 0;
for (i=1;i<=limit;i++)
{
count += i;
for(j=0;j< i;j++)
{
printf ("%d ",count-j);
}
printf("\n");
}
Related
First I have to input N, N becomes the first number to be checked.
Input: 79
Output should be: 537.70.
int sum=0;
while(1)
{
scanf("%d", &n);
if(n>=10 && n<80)
{
break;
}
printf("New output:\n");
}
for(i=n;i<=1000;i++)
{
if(i%2==0 && i%6!=0 && i%17!=0)
{
sum+=i;
}
I didnt put (float)sum/N to get average because I'm doing something wrong with sum.
More input output:
Input: 10 Output: 505.21
Input: 44 Output: 521.18
As well as keeping a 'running sum', you also need to keep a count of how many numbers were used, so you can properly calculate the average:
#include <stdio.h>
int main(void)
{
int n;
printf("Enter start number: ");
scanf("%d", &n);
int sum = 0, count = 0;
for (int i = n; i <= 1000; ++i) {
if (!(i % 2) && (i % 6) && (i % 17)) {
sum += i;
++count;
}
}
printf("Average is: %.2f\n", (double)sum / (double)count);
return 0;
}
Input: 79
Output should be: 537.70.
Are you sure about this value? I get 538.70 - but I get the given values for the other test cases you cite.
I wrote the following code in c-
/* Function to check whether the number is prime or composite*/
int prime(int a)
{
int i;
int count=0;
for(i=1;i<a;i++)
{
if (a%i==0)
{
count+=1;
}
}
if (a<=1)
{
return 0;
}
else if (count>1)
{
return 1;
}
else
{
return 2;
}
}
/* Code for the main function*/
int main()
{
printf("Enter your desired number");
int a;
scanf("%d",&a);
int i;
for(i=2;i<a;i++)
{
if (prime(i)==2 && prime(a-i)==2)
{
printf("The sum of %d and %d is %d\n",i,a-i,a);
}
}
return 0;
}
The problem i am getting is that the result for the number 16 comes as follows:
The sum of 3 and 13 is 16
The sum of 5 and 11 is 16
The sum of 11 and 5 is 16
The sum of 13 and 3 is 16
I don't want the cases to be repeated . Please help
Stop when you reach halfway. All factors will be symmetric after the halfway point.
#include <stdio.h>
int prime(int a)
{
int i;
int count=0;
for(i=1;i<a;i++)
{
if (a%i==0)
{
count+=1;
}
}
if (a<=1)
{
return 0;
}
else if (count>1)
{
return 1;
}
else
{
return 2;
}
}
int main()
{
printf("Enter your desired number");
int a;
scanf("%d",&a);
int i;
for(i=2;i<(a/2);i++)
{
if (prime(i)==2 && prime(a-i)==2)
{
printf("The sum of %d and %d is %d\n",i,a-i,a);
}
}
return 0;
}
Output:
Enter your desired number16
The sum of 3 and 13 is 16
The sum of 5 and 11 is 16
I did the code in c++, if you are not familiar with it, it is quite similar to c only for competitive coding.
Your logic is not good you might get a TLE (Time limit exceeded). Instead do this :
Store all your primes before hand you can do this in O(x*sqrt(x)) time,
and then use set to find the whether that number exists in your already created set of primes, you can do that in O(log(n)) time.
If you are having trouble understanding read about it here.
#include<bits/stdc++.h>
using namespace std;
set<int> primes;
int main()
{
int i,j,x;
cout<<"Enter your number: ";
cin>>x;
//Store all the primes before hand
for(i = 2; i<x; i++)
{
int square_root = sqrt(i);
for(j=2;j<=square_root;j++)
{
if(i%j==0)
break;
}
if(j > square_root) // j == (square_root + 1) i.e the loop never broke or it never found a number which could divide it hence i is a prime.
{
primes.insert(i);
}
}
//Now find whether it comprises of two primes or not
for (i=2; i<=x/2 ;i++)
{
if(primes.find(i) != primes.end() && primes.find(x-i) != primes.end())
{
cout<<"The sum of "<<i<<" and "<<(x-i)<<" is "<<x<<endl;
}
}
}
Expected output:
Enter your number: 16
The sum of 3 and 13 is 16
The sum of 5 and 11 is 16
and it also works for 4. :P
Enter your number: 4
The sum of 2 and 2 is 4
I need to make a program in {c} that would give me prime number for entered number (example is user enter 50. to write back 229)
So, I am stuck when making loop.
I am tring to define for row[100] to have row[0]=2,row[1]=3 and then I make i=4 and try to make a loop that would for number i devide number i with every single number in the row (becose those I know are prime numbers) and get module (number after 0,not sure how it is said on english), and then if if all of them have module !=0 then I know it is prime number and I want to add it into row.
So is there a way somebody can help me write this line? Thanks alot in advance :)
#include <stdio.h>
int main ()
{
int i,numb=4,position,temp,temp1,row[100];
printf(" enter position (1-100)\n");
scanf("%d",&position);
if (position>100||position<0 )
{
printf("error,enter position between 1 and 100");
return(0);
}
row[0]=2;
row[1]=3;
i=2;
do
{
temp=numb%2;
temp1=numb%3;
if (temp!=0 && temp1!=0)
{
row[i]=numb;
i++;
}
numb++;
}
while (i<100);
printf("%d. prime number is %d",position,row[position]);
return 0;
}
Ok,so I need to change part where I ask for module from deviding wit 2 and 3 to asking for module from deviding from all numbers in row at that moment. Thank you for help
#include <stdio.h>
#define MAX_N 100
int main(void){
int i, odd, temp, position, n = 0, row[MAX_N];
row[n++]=2;
row[n++]=3;
for(odd = 5; n < MAX_N; odd += 2){
int is_prime = 1;//true
for(i = 1; i < n; ++i){
temp = row[i];
if(temp * temp > odd)
break;
if(odd % temp == 0){
is_prime = 0;//false
break;
}
}
if(is_prime)
row[n++] = odd;
}
printf(" enter position (1-%d)\n", MAX_N);
scanf("%d", &position);
if (position > 100 || position < 1){
printf("error,enter position between 1 and %d\n", MAX_N);
return 0;
}
printf("%d. prime number is %d", position, row[position - 1]);
return 0;
}
Im working on an optional test review problem for an introduction to C class, and I need to have a program that prints out the following based upon what ever number a user enters:
Enter a number: 5
5
44
333
2222
11111
000000
11111
2222
333
44
5
So far this is the code that I have written
#include <stdio.h>
int main(void){
int row,column,space;
int number;
printf("Enter a number: ");
scanf_s("%d",&number);
for (row = 1; row <= number + 1; row++){
for (space = number; space >=row; space--){
printf(" ");
}
for(column = 1;column <= row; column++){
printf("%d",space);
}
printf("\n");
}
for (row = 1; row <=number;row++){
for(space = 1;space <= row;space++){
printf(" ");
}
for(column = number;column >=row;column--){
printf("%d",space);
}
printf("\n");
}
return 0;
}
This is the output that I get
Enter a number: 5
0
11
222
3333
44444
555555
22222
3333
444
55
6
I've spent quite a few hours trying to figure out how to print the upper half of the half diamond using the user entered numbers but I can't seem to figure it out. Could anyone point me in the right direction?
Your numbers are just off a bit, correct the printf calls and you're done:
First one:
printf("%d", number - space);
Second one:
printf("%d", space - 1);
A slightly better (more readable and a bit more logical) way would be to use other variables instead:
First one:
printf("%d", number + 1 - row);
Second one:
printf("%d", row);
Also note that some basic math can help you to realize the following:
Total number of rows: 2 * number + 1
Number of spaces: 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5 => abs(number - row) (If starting your rows with 0)
Number to print: Same as "Number of spaces"
Number count: 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 => 6 - number_of_spaces
This gives a much cleaner, more readable version with only one loop:
#include <stdio.h>
#include <math.h>
int main(void){
int row,column,space;
int number;
printf("Enter a number: ");
scanf_s("%d",&number);
for (row = 0; row <= number * 2; row++){
int number_of_spaces = abs(number - row);
int number_to_print = number_of_spaces;
int number_count = 6 - number_of_spaces;
for (space = 1; space <= number_of_spaces; space++){
printf(" ");
}
for(column = 1;column <= number_count; column++){
printf("%d", number_to_print);
}
printf("\n");
}
}
return 0;
}
To expand on schnaader's answer (which is perfectly fine and complete), you can improve your code even more, letting printf() do the spacing for you rather than doing a loop and several calls to printf():
printf("%*s", width, "");
Here width is replaced with the calculated space you'd like to fill. The precision * is a special placeholder that tells the function to take the actual precision/length from the parameter list. Since the string to print is empty, it will always fill the whole range with space characters.
for (row = 0; row <= number + number; row++) {
int t = row * (row <= number) + (number + number - row) * (row > number);
printf("%*c", number - t, ' ');
printf("%*d\n", t + 1, t);
}
There are numerous ways, I've just modified your code
#include <stdio.h>
int main(void){
int row,column,space;
int number;
printf("Enter a number: ");
scanf("%d",&number);
for (row = 1; row <= number + 1; row++){
for (space = number; space >=row; space--){
printf(" ");
}
for(column = 1;column <= row; column++){
printf("%d",number -space); //change1
}
printf("\n");
}
for (row = 1; row <=number;row++){
for(space = 0;space < row;space++){ //change 2
printf(" ");
}
for(column = number;column >=row;column--){
printf("%d",space);
}
printf("\n");
}
return 0;
}
the following code outputs the first half of the problem.
Notice the checking for errors in the call to scanf()
It compiles cleanly and works correctly
I leave it to you to complete the function for the second half of the output
which should be easy now that you have the first half
#include <stdio.h>
#include <stdlib.h>
int main()
{
int row;
int i; // loop counter/index
int n; // user input
printf( "\nEnter the wedge width: ");
if(1 != scanf(" %d", &n) )
{ // then, scanf failed
perror( "scanf failed" );
exit( EXIT_FAILURE );
}
// implied else, scanf successful
for( row = 0; row <= n; row++ )
{
// print n-row ' '
for( i=0; i<(n-row); i++)
{
printf(" ");
}
// print row+1 '(n-row)'
for( i=0; i<(row+1); i++)
{
printf( "%1d", (n-row));
}
printf( "\n" );
}
// reverse the calculations to print the lower half
return 0;
} // end function: main
I have a problem with a c program I'm trying to write. The program must store integers in array (read from the keyboard). The numbers must be printed out in the order of entering, for example if you enter: 3 2 0 5 5 5 8 9, the ouput should be:
3 2 0 - decreasing
5 5 5 - evenly
8 9 - increasing
The problem for me is, that I can't write an algorithm which to be able to work in all cases.
I was trying to "flag" the elements with another array(using the same index, to save for each integer a value 1-for increasing, -1-for decreasing and 0 for evenly), but this works partly.
Have you any other ideas?
Thanks in advance :)
#include <stdio.h>
#include <stdlib.h>
main() {
int array[100];
int flag[100];
int num, i;
printf("Enter how many numbers you want to type: ");
scanf("%d",&num);
for(i=0;i<num;i++) {
scanf("%d",&array[i]);
}
for(i=0;i<num;i++){
if((array[i]<array[i+1])) {
flag[i]=flag[i+1]=1;
}
if(array[i]>array[i+1]) {
flag[i]=flag[i+1]=-1;
}
}
for(i=0;i<num;i++) {
if(array[i]==array[i+1]) {
flag[i]=flag[i+1]=0;
}
}
for(i=0;i<num;i++){
printf("%d ",flag[i]);
}
printf("\n");
for(i=0;i<num;i++) {
if(flag[i]==1) {
do{
if(flag[i]==1){
printf("%d ",array[i]);
i++;
}
}while(flag[i]==1);
printf(" - increasing\n");
}
if(flag[i]==0) {
do{
if(flag[i]==0){
printf("%d ",array[i]);
i++;
}
}while(flag[i]==0);
printf(" - evenly\n");
}
if(flag[i]==-1) {
do{
if(flag[i]==-1) {
printf("%d ",array[i]);
i++;
}
}while(flag[i]==-1);
printf(" - decreasing\n");
}
}
system("pause");
return 0;
}
Thoughts:
You only know if the 'first' number belongs to a descending, even, or ascending sequence after you see the 'second' number.
The 'third' number either belongs to the same sequence as the first two or is the 'first' number of another sequence.
So: check two numbers and assign a sequence type.
Keep checking numbers in the same sequence.
When you cannot assign the same sequence go back to checking two numbers and assigning a sequence.
Something like
int *n1, *n2, *n3;
n1 = <first element>;
n2 = n1 + 1;
n3 = n2 + 1;
/* some kind of loop */
if ((n3 - n2) HAS_SOME_SIGN_AS (n2 - n1)) /* n3 belongs to the sequence */;
n1++;
n2++;
n3++;
/* end loop */
#include <stdio.h>
int status(a, b){
if(a < b) return 1;
if(a > b) return -1;
return 0;
}
int main() {
int array[100]={0};
int old_status, new_status;
int old_pos;
int num, i, j;
const char* status_message[] = {"decreasing","evenly","increasing", "i don't know"};
printf("Enter how many numbers you want to type: ");
scanf("%d",&num);
for(i=0;i<num;i++)
scanf("%d",&array[i]);
//{num | 1 < num <= 100}
old_status =status(array[0], array[1]);
old_pos = 0;
for(i=1;i<num;++i){
new_status = status(array[i-1], array[i]);
if(old_status != new_status){
for(j=old_pos;j<i;++j)
printf("%d ", array[j]);
printf("- %s\n", status_message[1 + old_status]);
old_status = (i<num-1)? status(array[i], array[i+1]):2;
old_pos = i;
}
}
if(old_pos < num){ //not output section output
for(j=old_pos;j<i;++j)
printf("%d ", array[j]);
printf("- %s\n", status_message[1 + old_status]);
}
return 0;
}