I am trying to get user input and store them in an array Fib[i]. After that print the Fib[i] array. When the user enters -1 the loop will quit and the program will end. But my code is not printing or terminating.
#include <stdio.h>
double Fib[50];//globally declared
int fib(int n)
{
int i;
for(i=0; i<50; i++)
{
scanf("%d", &Fib[i]);
if(i==-1)
break;
//printf("numbers entered %d\n", Fib[i]); // <- doesn't terminate if printf is here
}
printf("numbers entered %d\n", Fib[i]); //doesn't print anything??
}
int main()
{
int i, n;
//calling the function
fib(n);
return 0;
}
user input:
4
5
-1
Expected output:
Numbers entered
4
5
First issue: you declare Fib as an array of double:
double Fib[50];
But you use %d to read the values, which is for reading an int:
scanf("%d", &Fib[i]);
Using the wrong format specifier invokes undefined behavior. You presumably want to store integers, so change the array to int:
int Fib[50];
Next is your array breakout condition:
if(i==-1)
i is your array index, which ranges from 0 to 49, so this will never be true. You want to stop when the user enters -1, and that value will be in Fib[i]:
if(Fib[i]==-1)
Finally, printing the array:
printf("numbers entered %d\n", Fib[i]);
This doesn't print the array. It just prints the element at the last index of i, and the value at that index will always be -1. You need a separate loop to print the values:
int j;
printf("numbers entered:\n");
for (j=0; j<i; j++) {
printf("%d\n", Fib[j]);
}
This code has many code writing standard issues but it seems you are new so I am making minimal changes just for your understanding
#include <stdio.h>
double Fib[50];//globally declared
int fib(int n)
{
int i,j;
for(i=0; i<50; i++)
{
scanf("%lf", &Fib[i]);
if(Fib[i]==-1)
break;
}
printf("numbers entered \n");
for(int j=0;j<i;j++)
{
printf("%lf\n",Fib[j]);
}
}
int main()
{
int i, n;
fib(n);
return 0;
}
Related
I've searched for solutions to that problem and couldn't find any that match mine.
I wrote a program that gets two arrays of integers and return the scalar product between them. It works fine when I'm submitting the input manually, but when I try to read the input from a text file, I encounter that Segmentation fault.
Edit: I'm talking about stdin redirection
I would be grateful for some help.
The code is:
#include <stdio.h>
#define MAXLIMIT 100
int scalar_product(int[], int[], int);
void set_array(int[]);
int main(){
int arr1[MAXLIMIT], arr2[MAXLIMIT];
int size, result;
set_array(arr1);
set_array(arr2);
printf("Enter the vectors' dimension: ");
scanf("%d", &size);
result = scalar_product(arr1, arr2, size);
printf("The scalar product is: %d \n", result);
return 0;
}
void set_array(int a[]){
int i;
printf("Please enter a vector with up to %d elements: \n", MAXLIMIT);
for (i = 0; i < MAXLIMIT - 1 && (scanf("%d", &a[i]) != EOF); i++);
}
int scalar_product(int a1[], int a2[], int size){
int product = 0, i;
for (i = 0; i < size; i++){
product += a1[i] * a2[i];
}
return product;
}
and the text file contains:
1 -2 3 -4
6 7 1 -2
4
HEre
void set_array(int a[]) {
int i;
printf("Please enter a vector with up to %d elements: \n", MAXLIMIT);
for (i = 0; i < MAXLIMIT - 1 && (scanf("%d", &a[i]) != EOF); i++);
}
When reading from the console you will never hit EOF (unless you enter ctrl-D which I guess you didnt) so your set_array loops just keep going, reading from the file. You read all the data in the first set_array and read nothing in the second one because you have finished the input file
the actualk failure was that you ran off the end of the file, so the scanf of size failed and you were trying to read a random sized array in the function scalar_product.
Test the return from scanf always
What you need to do is put a count in the file before the first array so you know how many items to read into arr1 and I suggest a count before the second lot too.
ie
void set_array(int a[]) {
int i;
int count = 0;
printf("Please enter how many elements you want to enter, max = %d \n", MAXLIMIT);
scanf("%d", &count);
if(count > MAXLIMIT) count = MAXLIMIT;
for (i = 0; i < count && (scanf("%d", &a[i]) != EOF); i++);
}
I was trying to make a simple function to make a group of number that user enters them, using pointer of pointer but i keep getting this error and its hard to tell where the problem is, if there any other option to use something else that tells me where the problem is instead of this weird error.
#include <stdio.h>
void BuildGroub(int** group,int* count){
int i=0;
int j;
printf("Enter the size of the group \n");
scanf("%d", &*count);
while(*count != 0){
printf("Enter the %d number of the group:\n", i);
j=0;
scanf("%d", &**(group+i));
while(**(group+i)!=**(group+j)){
j++;
}
if(j==i){
i++;
count--;
} else{
printf("you have already entered this number please try again: \n");
}
}
}
int main(){
int count;
int group[100];
int *groupptr = &group;
BuildGroub(&groupptr,&count);
for(int i=0;i<count;i++){
printf("%d, ", group[i]);
}
return 0;
}
With this question, you do not need to use double pointer. If you want to learn how to use the double pointer, you can google then there are a ton of examples for you, for example, Double Pointer (Pointer to Pointer) in C.
In BuildGroub you decrease the count pointer
if(j==i){
i++;
count--;
}
, but in the condition of while loop, you compare the value that count pointer points to. it seems strange.
while(*count != 0)
Even if you change count-- to (*count)--, it will decrease the number of elements that you enter to 0 when you get out of the while loop, then in main function:
for(int i=0;i<count;i++){} // count is equal to 0 after calling BuildGroub function if you use (*count--) in while loop.
You should use a temp value for while loop function, for example:
int size = *count;
while(size != 0){
...
if (i == j) {
i++;
size--;
}
}
You should use, for example, group[i] instead of *(group+i). It will be easier to read your code.
The code complete:
#include <stdio.h>
void BuildGroub(int* group,int* count){
int i=0;
int j;
printf("Enter the size of the group \n");
scanf("%d", count);
int size = *count;
while(size != 0){
printf("Enter the %d_th number of the group:\n", i);
j=0;
scanf("%d", &group[i]);
while(group[i] != group[j]) {
j++;
}
if(j==i){
i++;
size--;
} else{
printf("you have already entered this number please try again: \n");
}
}
}
int main(){
int count;
int group[100];
int *groupptr = group;
BuildGroub(groupptr,&count);
for(int i=0;i<count;i++){
printf("%d, ", group[i]);
}
return 0;
}
The test:
./test
Enter the size of the group
5
Enter the 0_th number of the group:
1
Enter the 1_th number of the group:
2
Enter the 2_th number of the group:
2
you have already entered this number please try again:
Enter the 2_th number of the group:
3
Enter the 3_th number of the group:
3
you have already entered this number please try again:
Enter the 3_th number of the group:
4
Enter the 4_th number of the group:
5
1, 2, 3, 4, 5,
If you want to use a double pointer, you need to change your function like this:
void BuildGroub(int** group, int* count) {
int i = 0;
int j;
printf("Enter the size of the group \n");
scanf("%d", &*count); //I think this is redundant but works.
while (*count != 0) {
printf("Enter the %d number of the group:\n", i);
j = 0;
scanf("%d", (*group + i)); //The content of group + i
while ( *( *group + i) != *(*group + j)) { //the content of the content
j++;
}
if (j == i) {
i++;
(*count)--; //The content decrement
} else {
printf("you have already entered this number please try again: \n");
}
}
}
But you have a big problem in main and it is because you are using the parameter count to decrement until zero inside the function. So when the function finish, count value is zero and you don't print anything... You need to change this, using a internal variable to make the count, and finaly, setting the parameter to be using in main.
I was writing a C program to find inversions in an array. The program compiles smoothly but as soon as I run it, it displays a garbage value where I take the array as a input. The program is given below:
#include <stdio.h>
#include <stdlib.h>
int checkInversions(int arr[], int n) {
int i, j, inverse_count = 0;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
inverse_count++;
}
}
}
return inverse_count;
}
int main() {
int arr[10], i, n;
printf("Enter the elements of the array: %d");
for (i = 0; i <= 10; i++) {
scanf("%d", &arr[i]);
}
n = sizeof(arr) / sizeof(arr[0]);
printf("\n The inverse is: %d", checkInversions(arr, n));
return 0;
}
Now, when the statement Enter the elements of the array: is displayed, just beside that is a garbage value like 623089. I am able to take the input but the result is not correct. What is the cause of this? Any help in this regard will be appreciated.
You are calling printf with a format specifier for %d and nothing passed to satisfy the variable expected by the format string. This is undefined behavior.
What you meant to do was merely:
printf("Enter the elements of the array: ");
Also, since arr has 10 elements, you iterate through it as such:
for(i = 0; i < 10; i++)
You don't need to use sizeof to determine the size of the array since you already know it; it's 10.
I think you are missing the variable that should populate the %d on the printf.
Try taking out the %d on the printf call so it ends up like:
printf("Enter the elements of the array: ");
Or assign the corresponding variable to display with that "%d", like this:
printf("Enter the elements of the array: %d", variable);
Check if that helps!
Your problem is printf("Enter the elements of the array: %d");. You tell the program that you want to print an integer, but you do not specify which integer that is. Remove the %d and the garbage value will be gone, like this: printf("Enter the elements of the array: ");
The problem goes as follows:
Write a recursive function with three arguments: an array (a), the
number of elements of the array (n) and a number - k.
If k is positive and smaller than n it should print the first k positive
numbers of the array.
If k is negative and its absolute value is smaller than n the first k negative numbers should be printed.
If k is larger than n and positive all positive numbers should be printed.
If k is larger than n and negative all negative numbers should be
printed.
This is my attempt
#include <stdio.h>
#include <stdlib.h>
int function(int a[100], int n, int k)
{
int b[n], i;
if (k!=0 && n>1)
{
for (i=0; i<n; i++)
{
b[i]=a[i+1];
}
if (k<n || abs(k)<n)
{
if (k>0)
{
if (b[0]>0)
{
printf("%d", b[0]);
k=k-1;
}
}
if (k<0)
{
if (b[0]<0)
{
printf("%d", b[0]);
k=k+1;
}
}
return function(b, n-1, k);
}
}
else return 0;
}
int main()
{
int i, n, a[100], k;
printf("n = ");
scanf("%d", &n);
for (i=0; i<n; i++)
{
printf("a[%d] = ", i);
scanf("%d", &a[i]);
}
printf("k = ");
scanf("%d", &k);
printf("The new array is: \n");
function(a, n, k);
return 0;
}
But it only prints one number and I don't know how to fix it. Does anyone understand where I went wrong?
EDIT: If the array is {1, -1, 2, -2, 3, -3} and k=2, the expected result is {1, 2}. If k=-2 the expected result is {-1, -2}
EDIT 2: b is an array that contains all the elements of the a array other than a[0]
I found three issues with your code.
1) When I copied your code (converting to C# because that's what I work with), it threw an ArgumentOutOfRangeException for me here:
for (i=0; i<n; i++)
{
b[i]=a[i+1]; <------
}
When i is 99, it tries to copy a[100] into b[99], but there's no a[100]. You should change the for loop to this instead:
for (i=0; i<n-1; i++)
2) Your code is essentially treating a like a queue and popping the first number off (by copying all the others into a new array b), but instead of looking at that first number to determine whether or not to print it, you're skipping it and looking instead at the first item you just put into b:
if (k>0)
{
if (b[0]>0) <------
{
printf("%d", b[0]); <-----
k=k-1;
}
}
You should change your comparisons (and printf lines) to look at a[0] instead of b[0].
3) Whenever k is larger than n, then your entire logic block will get skipped (for example, I mistakenly set k to 34 instead of 3, and when I did, the if block reads if (34 < 6 || abs(34) < 6), which is always false.
Okay so I am trying to make my program ask for user integers (all working fine) and then call the function, which adds 5 to each array element, which i think is fine? then i need to print in the main program: the original integers with 5 added to them. anyone see what is wrong? my program crashes
#include <stdio.h>
void FUN(int ARR2[]);
int main()
{
int i=0;
int ARR1[20];
printf("Please enter 20 integers.\n");
for(i=0; i<20; i++) // switch back to 20
{
scanf("%i", &ARR1[i]);
}
FUN(&ARR1[i]);
printf("The new numbers are: %i", ARR1[i]);
return 0;
}
void FUN(int ARR2[])
{
int i=0;
ARR2[20];
for(i=0;i<20;i++)
ARR2[i]+=5;
}
When you reach this line
FUN(&ARR1[i]);
the value of your i variable is 20 so you are accessing the 21st element in ARR which is probably giving you an access violation. If you replace this line with
FUN(ARR1);
and remove the line
ARR2[20];
from your function FUN you might get the behaviour you are expecting
After your first for loop, i get's the value 20 but ARR1 is indexed 0 to 19. So, you might get a Segmentation Fault when you call
FUN(&ARR1[i]);
You should change that to
FUN(ARR1);
Also, in your function void FUN(int ARR2[]) , you have
ARR2[20];
Which also causes this error. Remove it.
You are also giving output the wrong way. You are doing
printf("The new numbers are: %i", ARR1[i]);
which also tries to access ARR1[20] , again causing problems.
You should change it to
printf("The new numbers are: \n");
for ( i=0 ; i < 20 ; i++)
printf("%d\n", ARR1[i]);
You should change your code to
#include <stdio.h>
void FUN(int ARR2[]);
int main()
{
int i=0;
int ARR1[20];
printf("Please enter 20 integers.\n");
for(i=0; i<20; i++) // switch back to 20
{
scanf("%i", &ARR1[i]);
}
FUN(ARR1);
printf("The new numbers are: \n");
for ( i=0 ; i < 20 ; i++)
printf("%d\n", ARR1[i]);
return 0;
}
void FUN(int ARR2[])
{
int i=0;
for(i=0;i<20;i++)
ARR2[i]+=5;
}