This question already has answers here:
Scan multiple integers without knowing the actual number of integers
(2 answers)
Closed 7 years ago.
Currently, I am asking user to specify number of input values being specified.
This the code for it:
#include<stdio.h>
#include<math.h>
#include<string.h>
void main()
{
int i,n;
printf("\nHow many record you will enter: ");
scanf("%d",&n);
float x[n];
printf("\n\nEnter the values of velocity (m/s):");
for(i=0; i<n; i++)
{
scanf("%f",&x[i]);
printf("\n%f",x[i]);
}
}
The code runs fine. But, I want to write code in such a way that it will calculate 'n' by scanning the input (numbers separated by space, not necessary one space between each number) without asking the user.
Can you suggest me a way for it.
PS: I am new to coding
Thanks in advance
You can have a look at fgets() and strtok(). Combining both of them , you can design as per your target. Also, you may need to know and use malloc() and free() to make use of dynamic memory allocation.
Maybe this answer can help you.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
The task is to calculate how many times a certain digit occurs in the entered sequence of numbers. The number of numbers to be entered and the number to be calculated are set by typing. Ask me if you have got question about code. The problem in finding a match with the number entered in the array.Can you give me hints or instructions, also i think about loop while but i don't know how to realize it please
The code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, b, n, c=0, arr[30];
printf("The count of numbers: ");
scanf("%d", &n);
printf("The number what is finding: ");
scanf("%d", &b);
for (i = 0; i < n; ++i)
{
scanf("%d", &arr[i]);
}
for(i=0;i < n;i++)
{
if(arr[i]=b)
{
c++;
printf("%d", c);
}
}
}
You should be compiling your code with at least some basic compilation flags. If you do, you will get a heads up that something is wrong before having to run it to find out. It saves a lot of time in the long run. Consult your compiler's documentation.
For instance, it would point out that your if condition is using an assignment (=) instead of an equality comparison (==). It should be:
if (arr[i] == b)
Also, you probably want to print out the total count at the end of the program - after the loop is finished. So move the printf("%d\n", c); after the loop. (You were also missing a newline which you probably wanted).
Also, scanf has a return value - you should check it. If the user enters invalid integers, you want to catch that and handle it properly.
Finally, since you declare your array to be of size 30, you should add a check that the desired length of the input array is no longer than that -- otherwise, you would get a buffer overflow.
Side note: please use more descriptive variable names. Not doing so often leads to confusion, especially for beginners. A small exception to this is for loop counters, like i in this case -- its perfectly fine to use a single letter. But consider b -- there is no obvious meaning; it should be something like target or to_find. Also, c could be count or total. As for n, perhaps size or length would be more suited.
if(arr[i]=b) it's wrong. x = y is an assignment but you want to do a check. To check if two elements are equal you should write if(arr[i] == b).
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I was thinking of a code to find the sum of numbers.
So i wrote bellow code :
#include <stdio.h>
#include <math.h>
int main()
{
int n,i,s=0,a[100];
printf("enter number of numbers");
scanf("%d",n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
s=0;
for (i=1;i<=n;i++)
{s=s+a[i];}
printf("sum is%d\n",s);
}
But in the output it shows segmentation error . I.e.
So whats the mistake?
So this line:
scanf("%d",n);
should be replaced by
scanf("%d",&n);
Explanation:
scanf() reads data from stdin according to format and store data in the location pointed by next additional argument. this case, format is %d means we want to read an integer number and this integer number will be stored in the location of n. The & operator is to get the location of a variable in C.
Use this :
scanf("%d",&n);
The reason :
You MUST put & in front of the variable used in scanf. The reason why will become clear once you learn about pointers. It is easy to forget the & sign, and when you forget it your program will almost always crash when you run it.
Examples :
scanf("%d %d", &a, &b);
printf("%d %d", a, b);
As a and b above are two variable and each has their own address assigned but instead of a and b, we send the address of a and b respectively. The reason is, scanf() needs to modify values of a and b and but they are local to scanf(). So in order to reflect changes in the variable a and b of the main function, we need to pass addresses of them. We cannot simply pass them by value.
But in case of printf function as we are only going to print the values of the variables in output console, there are no changes going to be made in variable a and b’s values. So it is not required to send their addresses.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have to take 'n' lines of character array(size m), which will have numbers only, and i have to put the numbers in a 2-d integer array.
I am getting segmentation fault while displaying the integer array for second time.
int t,m,n,i,j,pix[182][182];
char ch,pixel[183];
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m); //take n and m
for(i=0;i<n;i++){
printf("\n");
scanf("%s",pixel); //take character array
for(j=0;j<m;j++){
pix[i][j]=pixel[j]-48; //put numbers in integer array
dis[i][j]=0;
printf("%d ",pix[i][j]); //no error here
}
}
for(i=0;i<n;i++){
printf("\n");
for(j=0;j<m;j++)
printf("%d",pix[i][j]); //segmentation fault after n-1 lines are displayed
What is the problem?
Your code makes no effort to check for array boundaries. You need to check that m and n are less than 182 before proceeding. You should also use a method that protects against a buffer overrun when reading the pixel array - e.g fgets(pixel, sizeof(pixel), stdin). Otherwise, depending on the values on m and n you could have a buffer overrun with unpredictable results.
Your example also did not show the definition of the dis array, but you need to do a boundary check there as well.
Aside from that, I am guessing that your input is not laid out quite like your program expects. Performing array boundary checks and asserting as soon as they fail will help you find the discrepancy much quicker.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need a function that gets input in the form of int1, int2, ..., intn.
I neeed to store the values of these integers in an array. A separate function is used to get the number of integers to be read. How can I make the two functions work?
If it's not clear, it's something like this:
function1 gets an integer to get the number of input to be read. Then the function2 will read that input plus one but the input must be in a single line and must be separated by a comma and/or a white space.
Function1 gets, for example 5. function2 will want to read input like: 3, 21, 5, 1, 5, 2 and store it into a separate array for later use.
Can anyone help? Thanks. I thought of using loops but I remembered that the input must be in one line. Maybe scanf? With [^,]? But how do I make it work with the first function?
Try this:
#include <stdio.h>
void getInput(int sizeOfInput, int arr[]) {
int i = 0;
printf("IN");
for(; i < sizeOfInput - 1; ++i) {
scanf("%d, ", &arr[i]);
}
scanf("%d", &arr[i]);
printf("OUT");
}
main(){
int sizeOfInput = 0;
printf("Enter how many numbers do you want to enter?");
scanf("%d", &sizeOfInput);
int arr[sizeOfInput];
getInput(sizeOfInput, arr);
}
Sorry I am lazy but for you to learn it would be the best to figure out what this code does before you use it, that is also a reason why I did not comment it.
In doing a project for my computer science class, I came upon a problem with arrays. Basically, I have to take an input from the user for the number of years they want to input data for. I then have to use that input to create two arrays (one for the actual years they want the data for, and one for the data itself). I then have to print out the years and the data that the user just input.
The problem is that when I do this, the data for the years prints out fine, but the years themselves print out as random memory addresses. The other strang thing is that this only happens when I do three or more years. When I do two or less, everything else is fine. The variable for the years is an int and the variable for the data is a double.
int numberofyears;
printf("Enter the number of years you wish to take data for: " );
scanf("%d",&numberofyears); //take input for how many times arrays run
int years[numberofyears];
double dataforyear[numberofyears];
int a;
printf("Enter the years and their respective data");
for (a=0;a<numberofyears;a++){
scanf("%d %lf",&years[a],&dataforyear[a]);} //take inputs for both arrays
int b;
for (b=0;b<numberofyears;b++){
printf("%d %.2lf\n",years[b],dataforyear[b]);}
Input of:
Enter the number of of years you wish to take data for: 5
Enter the years and their respective data
1950 200.96
2000 300.55
Prints out:
1950 200.96
2000 300.55
Input of:
Enter the number of of years you wish to take data for: 5
Enter the years and their respective data
1956 325.21
1989 386.22
2003 400.00
Prints out:
0 325.21
1081671680 386.22
2003 400.00
And anything else beyond 3 years will do this. Though it seems like things beyond 5 years do not keep the last year like 3 does for whatever reason, if that helps.
How have you declared your dataforyear[] array?
If it is float dataforyear[] then change your scanf statement to
scanf("%d %f",&years[a],&dataforyear[a]);
else declare your array to double dataforyear[] if you want to keep yourscanf statement as it is.
These lines:
int years[numberofyears];
double dataforyear[numberofyears];
might be the problem. The compiler needs to know the size of your arrays at compile time. Otherwise you will have to allocate memory for your arrays. If your program isn't expected to read in a lot of data, you could always avoid allocating memory by specifying their sizes.
int years[10];
double dataforyear[10];