Counting number of items in an array in C [closed] - c

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 7 years ago.
Improve this question
I keep getting the output of 100 when I do what everyone else online has been posting about:
int total = sizeof(num)/sizeof(int);
It doesn't seem to work for me. Here's my code:
int main() {
static int num[100];
int totalNum = sizeof(num)/sizeof(int);
return 0;
}
void readNumbers(int* num) {
int i = 0;
FILE *fp;
fp = fopen("/Users/Documents/hello.txt", "r");
if (fp == NULL) {
printf("Could not load file");
exit(0);
}
/* Loads numbers into num array */
int number;
while(fscanf(fp, "%d", &number) > 0) {
num[i] = number;
i++;
}
}
My output is 100 so I'm assuming there isn't anything that is inserted into the num array? And if I print out sizeof(num) it gives me a hundred; 4 bytes * 100 = 400.
Here is what is in hello.txt:
14 21 39 48 109 3882

Unlike arrays in other languages (that can grow and shrink, and will tell you how many elements the array currently contains), a C array is just a simple block of memory that has a fixed size. You declared an array that can hold 100 elements, and that's all sizeof is going to tell you.
If you want to know how many numbers you've put in the array, then you have to keep track of that in a separate variable. The array itself doesn't contain that information.

Related

strtok inserting junk into an array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm having some issues with strtok, Im basically reading a file line by line, separating the data I get from the file with strtok and then storing it into an array.
A row in my file looks like this (I have several lines, but the idea is the same).
file.txt
3 0 5 0 0 8 0 1 0
Im doing the following
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void){
FILE* file;
char line[256];
char* data[9];
char* fileName = "file.txt";
file = fopen(fileName, "r");
fgets(line, sizeof(line), file);
char* ptr = strtok(line," ");
int i = 0;
while(ptr != NULL && i<9){
printf("%s",ptr);
data[i] = ptr;
i++;
ptr = strtok(NULL, " ");
}
for(int z = 0; z<i; z++){
printf( "%s", data[z]) ;
};
fclose(file);
}
Which gives me the output
3,0,5,0,0,8,0,1,0, (from the printf inside the while)
p��0�,�0�,�,,0,8,0,1,0 (from the data array)
Any idea on whats going on?
EDIT: Added spacing in txt file and separation between outputs. i<9 was added in while statement (i<8 made it so the last digit was not being catched).
EDIT2: Problem was fixed by using data[9] instead of data[8], being an array I thought the first element would be data[0] being data[8] the ninth.
Current output with code above:
3,0,5,0,0,8,0,1,0, (From ptr)
3,0,5,0,0,8,0,1,0, (From data array)
First of all you are allocating way too small buffer for your line. Use line[255] or even bigger. Next you are trying to print null pointers from data array and z<9 will lead to access out of bounds on index 8 as the last entry in data array is actually at index 7. So the best solution will be to limit for loop by i which actually equals size of read data entries.
for(int z = 0; z<i; z++){
printf( "%s", data[z]);
}
So you will be printing only non null entries from data array.
Last but not least as your data array can fit only 8 entries you need also change while loop to fit this limitation (or increase the size of the array):
while (ptr != NULL && i < 8) {
...
}

C Program stopped working after using scanf instead of var=0 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
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.
Improve this question
So I have this code:
#include <stdio.h>
#include <stdlib.h>
int functie ( int v[], int nr, int z)
{
int i,count;
scanf("%d", &z);
for(i=0;i<nr;i++) {
if(z==v[i]) {
count=count+1;
}
}
return z;
}
int main()
{
int i,nr,z;
fscanf("%d", &z);
FILE *f;
FILE *g;
f=fopen("data-in.txt", "r");
fscanf(f,"%d",&nr);
int *v=malloc(nr*sizeof(int));
for(i=0;i<nr;i++) {
fscanf(f,"%d",&v[i]);
}
g=fopen("data-out.txt", "w");
fprintf(g,"%d %d", functie(v,nr,z));
fclose(f);
fclose(g);
free(v);
}
and before I had int z=0; instead of trying to scanf my var z.
When I run this I get a stopped working error.
I have an array with numbers (integers) read from file, first line is the number of elements and second line the elements.
I need a var lets say z to check how many times an element appears in vector (check if scanned number z is equal to an element and +1 count )
example:
in file
4 (cuz I have 4 elements )
1 2 3 3 ( my elements )
than scan z as 3
z==3 true
count=1
again
count=2
Two issues here, both in main. First:
fscanf("%d", &z);
This function expects a FILE * for the first argument, which you didn't specify. Presumably you wanted to read from the console instead, so use scanf:
scanf("%d", &z);
Second issue:
fprintf(g,"%d %d", functie(v,nr,z));
Your format string is expecting two integer arguments, but you're only passing in one. Get rid of the extra format specifier:
fprintf(g,"%d", functie(v,nr,z));
fscanf will not be delimited by newlines. Best thing to do is eat through file a line at a time with fgets. So, add a line buffer to the top of main:
char line[16]; // assuming integers will actually be small
After opening your file as above with 'f':
if (fgets(line, sizeof(line), f))
{
int count = atoi(line);
int *v = (int *) malloc(count * sizeof(int));
for (int i = 0; i < count && fgets(line, sizeof(line), f); ++i)
{
v[i] = atoi(line);
}
}
You should absolutely add error checking to the above, which is minimal and for concept only (e.g. guard against bad input, buffer overflow, exceptions, count being wrong leaving you uninitialized integers at the end of the array, etc.) as this will only work if your input file is perfect. Note that 16 is generally a ridiculously small length for a line buffer. Also only do your logic against your vector if (a) the file opens, and (b) passes the first test, which is an integral number of entries.

how do i store a in an array in C? [closed]

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 5 years ago.
Improve this question
I'm writing a program that calculates results of football matches.I'm trying to store the team ids' in an array with a length of 10 defined at the top but i keep getting getting a build error from the array. I realise the syntax might be wrong but how else can i use a variable to specify array length?
The error message i'm getting is : expected expression before '{ ' token.
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 10
int main() {
int numberofmatches, hometeamid, awayteamid, hometeamgoals, awayteamgoals;
int hometeamwins = 0;
int winratio;
int teamid[ARRAY_SIZE];
printf("Enter number of matches played \n");
scanf("%d", &numberofmatches);
if (numberofmatches > 0) {
int x = 0;
do {
printf("Enter match stats in order Home_team_ID,Away_Team_ID,Goals_Home,Goals_Away\n");
scanf("%d %d %d %d", &hometeamid, &awayteamid, &hometeamgoals, &awayteamgoals);
teamid[ARRAY_SIZE] = {hometeamid}; //Error is on this line
if (hometeamgoals > awayteamgoals) {
hometeamwins++;
}
x++;
}
while (x < numberofmatches);
winratio = hometeamwins / numberofmatches;
printf(" %d :teamidth %d :winratio", teamid[0], winratio);
}
return 0;
}
This
teamid[ARRAY_SIZE] = {hometeamid};
is the syntax for defining an array of size ARRAYSIZE and initialising it incompletely.
You try it in the middle of a loop.
In case you want to write to an array member you probably want
teamid[x] = hometeamid;
Also, I recommend making sure that you do not write beyond teamid[9], which is the last legal member of the array, for ARRAY_SIZE == 10.
Your defining an array of size 10, and then accessing it at index 10. Since arrays are indexed 0..n-1 you need to access index 9 to get the end of the array instead of 10.

Filling an Array in C with 0 [closed]

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 5 years ago.
Improve this question
Ive got a problem with this code, im trying to add up array1 with array2.
I enter the numbers for array2 by Command line parameters.
When i enter 10 numbers it is working but when i add less than 10 I get an Memory access error.
My question is now: how do i fill up the missing array fields with the number 0? For example : I enter 9 numbers and the 10th field should be 0.
You are not checking how many command line arguments are passed, and when you index into the command line argument array, you will get an out-of-bounds error.
In you addiren function, you should take advantage of the argc that is passed and used that in your for loop limit.
#include <stdio.h>
#include <stdlib.h>
int addiren(int argc, char**argv){
int array_one[10] = {0,1,1,2,3,5,8,13,21,35};
int array_two[10] = {0}; //Quick way to set the array to all zeros
int array_three[10] = {0};
//Set array_two with your cmd-line args, notice the use of argc
for(int i = 1; i<argc && i<=10; i++){
array_two[i-1] = atoi(argv[i]);
}
//Add array_one with array_two to array_three
for(int i = 0; i<10; i++){
array_three[i] = array_one[i]+array_two[i];
}
//Return an int since that's what the function return type requires
return 0;
}
Hope this helps!

How to read information from a certain line in C? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a results.txt file and on the seventh line I have numbers in form like:
3 5 6 1 9 7 4
I want to gather information how many of them > 5 and how many of them < 5.
How can I do this process for all of them?
By the way, the seventh line is the last line of the file.
Just read one line at a time and count. When you reach 7, that's your seventh line. Use fgets. Once you have the line you can use strtol in a loop to read each value as an integer.
Reference: fgets, strtol
To skip 1 line in the input file:
fscanf(f, "%*[^\n]\n");
To read 1 number from the file:
int number;
fscanf(f, "%d", &number);
To compare a number with 5:
if (number < 5)
{
...
}
P.S. The site http://www.cplusplus.com has some examples for the basic stuff you need. That site is dedicated to C++ but at your level there so little difference between C and C++, you can use the examples for your work (if you understand them).
Example: fscanf (at the bottom of the page)
#include <stdio.h>
#define LINE_MAX 1024
int main() {
int line_count = 7;
int fd = open('file', r);
int smaller_than_five = 0, bigger_than_five = 0;
int number;
while (line_count != 0) {
fgets(input_line, LINE_MAX, fd);
line_count--;
}
while(sscanf(input_line, "%d", &number) != EOF) {
if (number > 5) bigger_than_five++;
else if (number < 5) smaller_than_five++;
}
/*
* Now you have:
* smaller_than_five which is the count of numbers smaller than five
* bigger_than_five which is the count of numbers bigger than five
*/
return 0;
}
This works when the numbers are on seventh line. If they are on the last (but could be second or 51st), you'll have to change the first while to read while you have not reached the end.

Resources