I don't get why sum == 105. For me it should be 100 but I can't get it to work that way. Can someone please explain?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int sum=0;
int t=0;
do{
t++;
sum= sum + t;
}
while(sum<100);
printf("sum:%d\n", sum); //prints 105
printf("t = %d\n", t);
return 0;
}
t starts at 0 and is incremented in each iteration of the loop.
sum starts at 0 and is increased by the value t in each iteration of the loop, until it is no longer smaller than 100. If we trace their values throughout the execution:
Iteration t sum
0 0 0
1 1 1
2 2 3
3 3 6
4 4 10
5 5 15
6 6 21
7 7 28
8 8 36
9 9 45
10 10 55
11 11 66
12 12 78
13 13 91
14 14 105
As you can see, 105 is the first value that sum gets that is not smaller than 100.
In your code: sum= 1+2+3+4+5+6+7+8+9+10+11+12+13+14 = 105.
Related
Given a Number N, Print the following pattern.
Input Format
The input contains a number N
Constraints
1 < N < 100
Output Format
The required pattern
for input 5 is
1
2 9
3 8 10
4 7 11 14
5 6 12 13 15
and for input 3 is
1
2 5
3 4 6
this is the code i have tried .. but the results are not the same
#include<stdio.h>
void pattern(int n)
{
for(int i=1; i<=n; i++)
{
int k = i;
for(int j=1; j<=i; j++)
{
printf("%d ",k);
k = n - j + k;
}
printf("\n");
}
}
int main()
{
int n = 5;
pattern(n);
return 0;
}
this is the result of the above code
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
How should I modify the above code to get the Expected Output?
This is an interesting problem, and not an easy one. I'm not going to write a program to solve it (in part because I'm too lazy), but I can describe how I would solve it.
You already have an outer loop for(int i=1; i<=n; i++) which counts down the rows, and an inner loop for(int j=1; j<=i; j++) which counts across the columns. Those are both fine.
Inside the inner loop I would test if(j % 2 == 1). If j % 2 is 1 we're in an odd-numbered column, and we want to count down the column. But if j % 2 is 0, we're in an even column, and we have to do it the other way.
First I would have a variable which is the number that's supposed to be at the top of the column (1, 9, 10, 14, or 15 in the n=5 case). I'd have to compute that number two different ways, one for the "odd" columns and one for the "even".
And then I'd use that number as a base to count down the odd columns, and up the evens. Specifically: I'd add i to it in the odd columns, but subtract i in the even columns. But actually that's not quite right, because i is not 1 at the top of columns other than 1, so what I'd actually have to add or subtract would be some function of i and j. But I think you can work this out.
Here are my three cents.:)
#include <stdio.h>
int main(void)
{
while ( 1 )
{
const unsigned int UPPER_LIMIT = 100;
printf( "Enter a non-negative number no greater than %u (0 - exit): ",
UPPER_LIMIT );
unsigned int n;
if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
if ( !( n < UPPER_LIMIT ) ) n = UPPER_LIMIT - 1;
putchar( '\n' );
for ( unsigned int i = 0; i < n; i++ )
{
for ( unsigned int j = 0; j < i + 1; j++ )
{
unsigned int value = j % 2 == 0
? i + 1 + j * n - j * ( j + 1 ) / 2
: ( j + 1 ) * n - j * ( j + 1 ) / 2 - i + j;
printf( "%2u ", value );
}
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
The program output might look the following way
Enter a non-negative number no greater than 100 (0 - exit): 10
1
2 19
3 18 20
4 17 21 34
5 16 22 33 35
6 15 23 32 36 45
7 14 24 31 37 44 46
8 13 25 30 38 43 47 52
9 12 26 29 39 42 48 51 53
10 11 27 28 40 41 49 50 54 55
Enter a non-negative number no greater than 100 (0 - exit): 9
1
2 17
3 16 18
4 15 19 30
5 14 20 29 31
6 13 21 28 32 39
7 12 22 27 33 38 40
8 11 23 26 34 37 41 44
9 10 24 25 35 36 42 43 45
Enter a non-negative number no greater than 100 (0 - exit): 8
1
2 15
3 14 16
4 13 17 26
5 12 18 25 27
6 11 19 24 28 33
7 10 20 23 29 32 34
8 9 21 22 30 31 35 36
Enter a non-negative number no greater than 100 (0 - exit): 7
1
2 13
3 12 14
4 11 15 22
5 10 16 21 23
6 9 17 20 24 27
7 8 18 19 25 26 28
Enter a non-negative number no greater than 100 (0 - exit): 6
1
2 11
3 10 12
4 9 13 18
5 8 14 17 19
6 7 15 16 20 21
Enter a non-negative number no greater than 100 (0 - exit): 5
1
2 9
3 8 10
4 7 11 14
5 6 12 13 15
Enter a non-negative number no greater than 100 (0 - exit): 4
1
2 7
3 6 8
4 5 9 10
Enter a non-negative number no greater than 100 (0 - exit): 3
1
2 5
3 4 6
Enter a non-negative number no greater than 100 (0 - exit): 2
1
2 3
Enter a non-negative number no greater than 100 (0 - exit): 1
1
Enter a non-negative number no greater than 100 (0 - exit): 0
Or using the recursive approach of calculating the output value the program can look the following way.
#include <stdio.h>
int main(void)
{
while ( 1 )
{
const unsigned int UPPER_LIMIT = 100;
printf( "Enter a non-negative number no greater than %u (0 - exit): ",
UPPER_LIMIT );
unsigned int n;
if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
if ( !( n < UPPER_LIMIT ) ) n = UPPER_LIMIT - 1;
putchar( '\n' );
for ( unsigned int i = 0; i < n; i++ )
{
unsigned int value = i + 1;
for ( unsigned int j = 0; j < i + 1; j++ )
{
printf( "%2u ", value );
value += j % 2 == 0 ? 2 * ( n - i ) - 1 : 2 * ( i - j );
}
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
For example its output for the entered number equal to 10 looks like
Enter a non-negative number no greater than 100 (0 - exit): 10
1
2 19
3 18 20
4 17 21 34
5 16 22 33 35
6 15 23 32 36 45
7 14 24 31 37 44 46
8 13 25 30 38 43 47 52
9 12 26 29 39 42 48 51 53
10 11 27 28 40 41 49 50 54 55
Enter a non-negative number no greater than 100 (0 - exit): 0
When printing each Fibonacci sequence the first couple of sequences print in weird symbols or not at all if printing more than 8 sequences.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
//n=amount of numbers in the series to compute, seq=array to store series
void fibonacci(int n, int* seq){
// Complete this function
int i;
seq[0] = 0;
seq[1] = 1;
for(i = 2; i <= n; i++){
seq[i] = seq[i-2] + seq[i-1];
}
}
int main(){
int n;
//n, amount of series to compute
scanf("%d",&n);
//initialize array to 1, using malloc/calloc
int *seq = malloc(1 * sizeof(*seq));
int i;
for(i = 1; i <= n; i++){
//recompute the whole series
fibonacci(i, seq);
//print array
int j;
for(j = 0; j < i; j++)/* complete code */
printf("%d ", seq[j]);
//resize array, with realloc
int newSize=i+1;
int *seq = realloc(seq, newSize);
printf("\n");
}
//free array
return 0;
}
Output:
"7Y��yb�=
Um�*/E�o 1 1 2 3 5 8 13
0 1 1 2 3 5 8 13 21
0 1 1 2 3 5 8 13 21 34
0 1 1 2 3 5 8 13 21 34 55
0 1 1 2 3 5 8 13 21 34 55 89
0 1 1 2 3 5 8 13 21 34 55 89 144
0 1 1 2 3 5 8 13 21 34 55 89 144 233
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946
There are different problems in your code:
In your fibonacci() function, you iterate using i <= n, but inside the loop, you assign to seq[i]. When i = n, this becomes a problem: you're accessing one cell out of the array.
You are getting n from user input, but then doing int *seq = malloc(1 * sizeof(*seq)). You are only allocating space for one element, not n. You should do malloc(n * sizeof(*seq)) instead.
Not really an error, but inside the first for loop in your main, you're both re-defining and re-allocating the seq array with int *seq = realloc(...). That is not needed at all. Your array is already n cells big, so there is no need to reallocate it each time. You can use it as is.
Not really an error, but there is no need to recompute the series each time. You can compute it only once and then partially print it on each row without a problem.
Also, IMPORTANT! Using int to hold numbers of the Fibonacci sequence is only good until you reach n = 47. More than that, and your next element will overflow the maximum positive value that an int can hold, turning negative, and invalidating the rest of the calculations too. I would suggest you to use long long unsigned int instead, which would be good up to n = 94 (assuming 64 bits). Ultimately, you should check the value of n before calculating the Fibonacci sequence to avoid an overflow.
Here's a better version of your code with those problems fixed:
void fibonacci(int n, int* seq) {
int i;
seq[0] = 0;
seq[1] = 1;
for(i = 2; i < n; i++)
seq[i] = seq[i-2] + seq[i-1];
}
int main() {
int *seq;
int n, i, j;
scanf("%d",&n);
// Allocate enough space for n elements:
seq = malloc(n * sizeof(*seq));
// Compute the whole series once:
fibonacci(n, seq);
// Print partial series on each row:
for(i = 1; i <= n; i++) {
for(j = 0; j < i; j++)
printf("%d ", seq[j]);
printf("\n");
}
free(seq);
return 0;
}
I have this text file:
2 6
99 100 14 15 1 4 29 43 15 15
31 24 2 0 2 0 2 0 12 12
1 5 2 6 3 50 2 0 1 100
31 24 2 0 2 0 2 0 12 12
99 100 14 15 1 4 29 43 15 15
Lucky 0 0 100 100
James 2 0 100 100
Jerry 2 4 100 100
Cristofor 0 2 100 100
Chris 2 3 100 100
Miclaus 2 1 100 100
I want to read all data in multiple structures.
This is my code: I read first 6 lines and i want to read next 6 and put every word in variables. I don t know how to read that string.
void citireDate(){
char c;
FILE *f;
f = fopen("nume.in","r");
fscanf(f,"%d", &R);
fscanf(f,"%d", &P);
for(int i=1;i <= 2*R+1 ; i++)
for(int j=1;j <= 2*R+1; j++){
fscanf(f,"%d",&ghetar[i][j].inaltime);
fscanf(f,"%d",&ghetar[i][j].manusi);
}
for(int i=1;i<=6;i++){
//here i have to read that string
fscanf(f,"%d",&spiridusi[i].x);
fscanf(f,"%d",&spiridusi[i].y);
fscanf(f,"%d",&spiridusi[i].hp);
fscanf(f,"%d",&spiridusi[i].stamina);
}
fclose(f);
}
How can i read that string?
just read all the line in one fscanf(f, "%s %d %d %d %d\n", ...);
for(int i=1;i<=6;i++) {
fscanf(f, "%s %d %d %d %d\n",
&spiridusi[i].?????,
&spiridusi[i].x,
&spiridusi[i].y,
&spiridusi[i].hp,
&spiridusi[i].stamina);
}
Or course I suppose there is no space in the string before the 4 integers
It is strange the first indexes are always 1, I think they must be 0 in all the for (except if you have a good reason to start by 1)
I am trying to 4x4 input in an 5x5 array and get the sum of each lines on the fifth lines.
I'm sure if you read my code below, you'll know what I am trying to talk about.
For example if I type in:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
The expected Result should be:
1 2 3 4 10
5 6 7 8 26
9 10 11 12 42
13 14 15 16 58
28 32 36 40 136
Instead, I am getting a result like:
1 2 3 4 10
5 6 7 8 32792
9 10 11 12 42
13 14 15 16 58
28 32 36 40 -501277720
I thought about why I get these random values, but couldn't find a solution. Why am I getting these values and what can I do to solve it?
#include <stdio.h>
int main ()
{
int gradeArr[5][5];
int i,j;
printf("Input grades:\n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
scanf("%d",&gradeArr[i][j]);
gradeArr[i][4] += gradeArr[i][j];
}
}
printf("%d\n", gradeArr[1][4]);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
gradeArr[4][i] += gradeArr[j][i];
}
gradeArr[4][4] += gradeArr[4][i];
}
printf("Result: \n");
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf("%d ",gradeArr[i][j]);
}
printf("\n");
}
return 0;
}
You need to initialize the array.
Try
int gradeArr[5][5] = {0};
I am trying to make a program that takes 3 inputs from the user: row/columns count and number range start and number range end.
In my case and for example lets say 4 1 16 so it means 4 rows and columns print numbers from 1-16.
I have a problem accomplishing this.
#include <stdio.h>
#include <stdlib.h>
int num1,num2,count,i,y;
int main()
{
count = 4;
num1 = 1;
num2 = 16;
for(i=1; i<=count; i++){
for(y=num1; y<=num2; y++){
printf("%d ",y);
}
printf("\n");
}
return 0;
}
The output is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Whereas I want my output to be:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
for(y=1; y<=count; y++){
printf("%d ",num1++);
}
That way you will ensure that all the numbers will be printed. you can use num1 variable that you have declared and initialized with 1.
Don't use unnecessary global variables. And keep the declarations close to where you use them. That way you won't have to look in the top of the page to check the type or if you initialized or not.
for(i=1; i<=count; i++){
printf("%d ",i);
if( i % 4 == 0)
printf("\n");
}
Print 4*(i-1)+y instead of y:
printf("%d ",4*(i-1)+y);