A Fibonacci program that I wrote is not working correctly. This program should display the nth and nth series but this is what happened: the resulting output is:
1 8 13 21 34 55 1284926876 32762 -440528208 678 1285217672
Whereas it should be the 5th to 15th Fibonacci:
1 8 13 21 34 55 89 144 233 377 610 987
Here is the code:
#include <stdio.h>
int main() {
int jbf, c, a, b, m, s;
int hasil[100];
printf("============================ \n");
printf("| Program Fiboonanci ");
printf("| \n");
printf("============================ \n");
printf("enter number of rows : ");
scanf("%d", &jbf);
printf("starting from line: ");
scanf("%d", &m);
s = jbf + m;
if (jbf > 0) {
a = 0;
b = 1;
printf("%d ", b);
for (int i = 1; i < jbf; i++) {
c = a + b;
hasil[i] = c;
a = b;
b = c;
}
for (int i = m; i < s; i++) {
printf("%d ", hasil[i]);
}
} else {
printf("enter a value > 0");
}
}
You have
scanf("%d", &jbf);
for ( int i = 1; i < jbf; i++) {
s = jbf + m;
for ( int i = m; i < s; i++) {
it looks to me that s > jbf so you also print values which were not calculated. I would also check for s>99, you have int hasil[100];, but the indexes in that field are 0..99 not 1..100, but you index from 1, which is fine as long as you remember that you allocated space for 100 int values, but you can use only 99 of them right now.
Related
I'm trying to solve the hacker rank question "Small triangle, Large triangle"
I have to arrange triangle by increasing order of area
the code I wrote is working fine on smaller input given by me but fails on the larger inputs, after taking inputs I just get terminated
eg,
working on input
3
7 24 25
5 12 13
3 4 5
not working when
10
67 67 19
3 57 55
33 33 49
61 58 59
23 43 35
48 42 45
23 12 27
41 34 22
26 49 35
63 46 45
unable to understand why!! Thank YOU!! in advance
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct triangle
{
int a;
int b;
int c;
};
typedef struct triangle triangle;
void sort_by_area(triangle *tr, int n)
{
int *p = (int*)malloc(n * sizeof(int));
int *middle = (int*)malloc((3*n)*sizeof(int));
// doing sum of side of triangle
for (int i = 0; i < n; i++)
{
p[i] = tr[i].a + tr[i].b + tr[i].c;
}
// arranging sum in increasing order
// as more the sum of a+b+c more its area in herons formula
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (p[i] < p[j])
{
int temp;
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
int num = 0;
// storing sides in increasing order in middle
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (p[i] == tr[j].a + tr[j].b + tr[j].c)
{
middle[num] = tr[j].a;
num++;
middle[num] = tr[j].b;
num++;
middle[num] = tr[j].c;
num++;
}
}
}
num=0;
// copying increassed order in tr poiinter in question
for (int i = 0; i < n; i++)
{
tr[i].a = middle[num];
num++;
tr[i].b = middle[num];
num++;
tr[i].c = middle[num];
num++;
}
}
int main()
{
int n;
scanf("%d", &n);
triangle *tr = malloc(n * sizeof(triangle));
for (int i = 0; i < n; i++) {
scanf("%d%d%d", &tr[i].a, &tr[i].b, &tr[i].c);
}
sort_by_area(tr, n);
for (int i = 0; i < n; i++) {
printf("%d %d %d\n", tr[i].a, tr[i].b, tr[i].c);
}
return 0;
}
It's kind of strange that your function is called sort_by_area but it doesn't seem to calculate any area. Instead it seems to calculate the perimeter of the triangles.
Anyway... there is a bug here. (note: There may be other bugs but to start with you need to fix this)
int *middle = (int*)malloc((3*n)*sizeof(int));
...
...
int num = 0;
// storing sides in increasing order in middle
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (p[i] == tr[j].a + tr[j].b + tr[j].c)
{
middle[num] = tr[j].a;
num++;
middle[num] = tr[j].b;
num++;
middle[num] = tr[j].c;
num++;
}
}
}
You have two nested loops going from 0 to n. When the if statement is true, you increment num 3 times.
So in case p[i] == tr[j].a + tr[j].b + tr[j].c is always true (which is possible), you end up with num being equal to 3*n*n.
And you use num as index into middle but middle is only allocated to hold 3*n integers.
So you are writing outside the allocated area. That's undefined behavior.
BTW
When sorting arrays in C you should nearly always use qsort
The code works fine but why are my answers wrong in the int result?
in output:
3
10
2 3 5 7: 17 //correct
30
2 3 5 7 11 13 17 19 23 29: 146 //incorrect
50
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47: 474 //incorrect
Here is the code:
#include <stdio.h>
int main() {
int y, n, i, fact, j, result = 0;
scanf("%d", &y);
for (int x = 1; x <= y; x++) {
scanf("%d", &n);
for (i = 1; i <= n; i++) {
fact = 0;
for (j = 1; j <= n; j++) {
if (i % j == 0)
fact++;
}
if (fact == 2) {
result += i;
printf("%d ", i);
}
}
printf(": %d\n", result); //Not Getting correct answer please HELP!
}
return 0;
}
You forgot to initialize result before each calculation.
for(int x=1;x<=y;x++){
scanf("%d", &n);
result = 0; // add this for initialization
for (i = 1; i <= n; i++) {
/* ... */
}
/* ... */
}
The variable result is initialized only once
int y, n, i, fact, j, result = 0;
So it will accumulate values calculated in the loop
for(int x=1;x<=y;x++){
//...
}
Move the declaration of the variable result in the body of the loop
for(int x=1;x<=y;x++){
int result = 0;
//...
}
To avoid such an error you should declare variables in the minimum scope where they are used.
Also this loop
for (j = 1; j <= n; j++)
{
if (i % j == 0)
fact++;
}
does not make a great sense. Change the condition in the loop the following way
for (j = 1; j <= i; j++)
{
if (i % j == 0)
fact++;
}
substituting the variable n for the variable i.
Also you should use an unsigned integer type instead of the signed integer type int because prime numbers are defined for natural numbers.
The program can look for example the following way
#include <stdio.h>
int main(void)
{
unsigned int n = 0;
scanf( "%u", &n );
while ( n-- )
{
unsigned int max_value = 0;
scanf( "%u", &max_value );
unsigned long long int sum = 0;
for ( unsigned int i = 1; i <= max_value; i++ )
{
size_t count = 0;
for ( unsigned int j = 1; j <= i; j++ )
{
if ( i % j == 0 ) ++count;
}
if ( count == 2 )
{
printf( "%u ", i );
sum += i;
}
}
printf( ": %llu\n", sum );
}
return 0;
}
If to enter
3
10
20
100
then the output will be
2 3 5 7 : 17
2 3 5 7 11 13 17 19 : 77
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 : 1060
I wrote this code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char numbers[2001];
char a[1000], b[1000];
int int1, int2, i, n = 0;
int sum, difference;
fgets(numbers, sizeof(numbers), stdin);
for (i = 0; i < 1000; i++)
{
if (numbers[i] != ' ')
{
a[i] = numbers[i];
}
else if (numbers[i] == ' ') {
i += 1;
b[n] = numbers[i];
for (n = 1; n < 1000; n++)
b[n] = numbers[n+i];
}
}
int1=atoi(a);
int2=atoi(b);
sum = int1 + int2;
difference = int1 - int2;
printf("%d\n%d", sum, difference);
return 0;
}
But there are several lines of 2 numbers in input file. And I want the program to find the sum and difference for each line and print them.
Here is an example of an input:
12 45
36 111
9 5
153 6
output:
57
33
147
-75
14
4
159
147
You can use sscanf for getting the number from the string, it's more simple:
FILE * fp = fopen("input.txt", "r");
if (!fp) {
return -1;
}
int i = 0;
while(fgets(numbers, sizeof(numbers), fp)) {
sscanf(numbers, "%d %d", &a[i], &b[i]);
printf("sum = %d\n", a[i]+b[i]);
printf("diff = %d\n", abs(a[i] - b[i]));
i++;
}
You should change the type of a and b from char to int for the big number.
int a[1000], b[100];
Because the numbers in my code is the line of the input file, and it content of 2 number, so you can decrease its size:
char numbers[256]; // for example.
The complete for test:
int main(void) {
char numbers[256];
int a[1000], b[1000];
FILE * fp = fopen("input.txt", "r");
if (!fp) {
return -1;
}
int i = 0;
while(fgets(numbers, sizeof(numbers), fp) && i < 1000) {
sscanf(numbers, "%d %d", &a[i], &b[i]);
i++;
}
for(int j = 0, j < i; j++) {
printf("%d %d\n", a[j], b[j]);
printf("sum = %d\n", a[j]+b[j]);
printf("diff = %d\n", abs(a[j] - b[j]));
}
return 0;
}
The input and output:
#cat input.txt
12 45
36 111
9 5
153 6
./test
12 45
sum = 57
diff = 33
36 111
sum = 147
diff = 75
9 5
sum = 14
diff = 4
153 6
sum = 159
diff = 147
I need to make a program which sorts out a given array in such a way that the numbers with the same digits will be in front. The order must remain the same. It would be a big problem but I'm not allowed to use any addiodional arrays, functions etc. I really don't know how to sort out the numbers so that the order remains the same. And also the array is max 100 elements.
Example:
Input:
1 22 43 444 51 16 7 8888 90 11
Output:
1,22,444,7,8888,11,43,51,16,90.
I've written this so far:
#include <stdio.h>
int main()
{
int a = 0, i = 0, niz[100], temp, N, j, logika, cifra1, cifra2, brojac = 0, brojac2 = 1;
printf("Unesite brojeve: \n");
do {
scanf("%d", &niz[i]);
if (niz[i] == -1) {
i--;
break;
}
i++;
} while (i < 100);
N = i;
for (i = 0; i < N; i++) {
a = niz[i];
logika = 1;
cifra1 = a % 10;
cifra2 = niz[i] / 10;
while (cifra2) {
if (cifra2 % 10 != cifra1) {
logika = 0;
break;
}
cifra2 = cifra2 / 10;
}
if (a / 10 == 0) logika = 1;
if (logika == 1) {
niz[brojac++] = niz[i];
}
if (logika == 0) {
niz[i] = temp;
niz[N - 1] = niz[i];
niz[N - i] = temp;
}
}
printf("Nakon preslaganja niz glasi: \n");
for (i = 0; i <= N; i++) {
if (i < N)
printf("%d,", niz[i]);
else {
printf("%d.", niz[i]);
}
}
return 0;
}
#include <stdio.h>
enum { MAX_NUMBERS = 100 };
int main(void)
{
int numbers[MAX_NUMBERS];
int numbers_count = 0;
int input;
puts("Please enter your numbers, end input with -1:");
while (numbers_count < MAX_NUMBERS && scanf(" %d", &input) == 1 && input != -1)
numbers[numbers_count++] = input;
int current_insertion_point = 0; // where to insert
for (int i = 0; i < numbers_count; ++i) {
int all_digits_the_same = 1;
// check if number only contains one probably repeated digit:
int current_number = numbers[i];
int last_digit = -1;
while (current_number && all_digits_the_same) {
int current_digit = current_number % 10;
current_number /= 10;
if (last_digit != -1 && current_digit != last_digit)
all_digits_the_same = 0;
last_digit = current_digit;
}
// insert the number:
if (all_digits_the_same) {
int temp = numbers[i];
// nove the range current_insertion_point ... i to the right
for (int k = i; k > current_insertion_point; --k)
numbers[k] = numbers[k - 1];
// so there is space to insert the number previously numbers[i]
numbers[current_insertion_point++] = temp;
}
}
for (int i = 0; i < numbers_count; ++i)
printf("%d ", numbers[i]);
putchar('\n');
}
Output:
Please enter your numbers, end input with -1:
1 22 43 444 51 16 7 8888 90 11 -1
1 22 444 7 8888 11 43 51 16 90
I have done selection sort.It's not giving output as you expecting exactly.
#include<iostream>
using namespace std;
int main(){
int x;
cout<<"Enter Size Of Array";
cin>>x;
const int SIZE=x;
int a[SIZE];
int copy[SIZE];
cout<<"Enter "<<SIZE<<" Value";
for(int i=0;i<SIZE;i++){
cin>>a[i];
}
for(int i=0;i<SIZE;i++){
copy[i]=a[i];
}
for(int i=0;i<SIZE;i++){
int n=a[i];
int temp;
int rem=n%10;
bool isAllDigitSame=false;
while(n!=0){
if(rem==n%10){
isAllDigitSame=true;
}
else{
isAllDigitSame=false;
break;
}
n=n/10;
}
if(isAllDigitSame){
temp=a[i];
a[i] = a[i]%10;
}
}
cout<<"A="<<endl;
for(int i=0;i<SIZE;i++){
cout<<a[i]<<endl;
}
cout<<endl;
for(int i=0;i<SIZE;i++){
int min=i;
for(int j=i;j<SIZE;j++){
if(a[j]<a[min]){
min=j;
}
}
int temp;
temp=copy[i];
copy[i]=copy[min];
copy[min]=temp;
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
cout<<"Copy="<<endl;
for(int i=0;i<SIZE;i++){
cout<<copy[i]<<endl;
}
return 0;
}
It is sorting other element also.
Input :1 22 43 444 51 16 7 8888 90 11
Output: 1 11 22 444 7 8888 16 43 51 90
#include <stdio.h>
int prime(int limit,int col);
int main(){
int limit,col,count,i;
printf("Table of Primes\n");
printf("===============\n");
printf("Upper limit: ");
scanf("%d",&limit);
getchar();
printf("# of columns: ");
scanf("%d",&col);
count=prime( limit,col);
return 0;
}
int prime(int limit,int col){
int i,j,w;
for(w=0;w<col;w++){
for(i=2;i<=limit;i++){
for(j=2;j<=i;j++){
if(i%j==0){
break;
}
}
if(i==j){
printf("%d ",i);
}
}
printf("\n");
}
}
The code above is to find the amount of prime numbers between 2 and the user input numbers.
I have that working fine, but my issue is getting the Code to be put into columns (defined by # of columns)
I talked this over with my teacher and she said that I do not have to use a 2D array to accomplish this.
Can anyone please help me out?
Also the output is spouse to look like this:
Table of Primes
===============
Upper limit: 175
# of columns: 5
2 3 5 7 11
13 17 19 23 29
31 37 41 43 47
53 59 61 67 71
73 79 83 89 97
101 103 107 109 113
127 131 137 139 149
151 157 163 167 173
Thank you.
You can easily test the number of columns and print a newline. The function prime() also lacked a return value so I return w as the number of primes found. This is just a slight tweak to your prime() function.
#include <stdio.h>
#include <string.h>
int prime(int limit, int col) {
int i, j, w = 0;
for (i=2; i<=limit; i++) {
for (j=2; j<=i; j++) {
if (i%j == 0) {
break;
}
}
if (i==j) {
if (w++ % col == 0) // test number of columns
printf("\n");
printf("%7d", i); // specify field width
}
}
printf("\n");
return w;
}
int main(void) {
prime (173, 5);
return 0;
}
Program output:
2 3 5 7 11
13 17 19 23 29
31 37 41 43 47
53 59 61 67 71
73 79 83 89 97
101 103 107 109 113
127 131 137 139 149
151 157 163 167 173
I leave you to add the frills.
I suggest you to make separate functions for finding primes and displaying primes, so you will have more control on displaying those primes:
#include <stdio.h>
#include <math.h> // sqrt
int is_prime (int x);
void prime(int limit, int col);
int main ()
{
int limit, col;
printf("Table of Primes\n");
printf("===============\n");
printf("Upper limit: ");
scanf("%d",&limit);
getchar();
printf("# of columns: ");
scanf("%d",&col);
prime(limit, col);
printf("\n");
return 0;
}
int is_prime (int num)
{
int flag;
int i;
double p;
p = sqrt(num);
flag = 1;
if (num == 1)
return 0;
if (num == 2)
return flag;
if ((num % 2) == 0)
return 0;
for (i = 3; i <= p; i += 2)
{
if ((num % i) == 0)
{
flag = false;
return flag;
}
}
return flag;
}
void prime (int limit, int column)
{
int i, j, cnt;;
cnt = 0;
for (i = 0; i < limit; i++) {
if (is_prime(i)) {
printf("%4d ", i);
cnt++;
if ((cnt % column) == 0) {
printf("\n");
}
}
}
}
Of course you can change is_prime to your own function.
You can keep a variable to count how many numbers have been printed, then count % col will be zero every time count is a multiple of col, so this would work
#include <stdio.h>
void prime(int limit, int col);
int readint(const char *const message);
int main()
{
int limit,col;
printf("Table of Primes\n");
printf("===============\n");
limit = readint("Upper limit");
col = readint("# of columns");
prime(limit, col);
return 0;
}
int readint(const char *const message)
{
int value;
printf("%s> ", message);
while (scanf("%d", &value) != 1)
{
int chr;
printf("\tinvalid input\n");
printf("%s> ", message);
do {
chr = getchar();
} while ((chr != EOF) && (chr != '\n'));
}
return value;
}
void prime(int limit, int col)
{
int i, j;
int count;
count = 0;
for (i = 2 ; i <= limit ; i++)
{
for (j = 2 ; j <= i ; j++)
{
if (i % j == 0)
break;
}
if (i == j)
{
count += 1;
printf("%5d", i);
if ((count != 0) && (count % col == 0))
printf("\n");
}
}
}
Note that your code does not handle invalid input, I added that in this code, many people erroneously ignore scanf()'s return value, which is there for a reason.