I'm a little lost when it comes to initializing a dynamic array. I am counting the number of integers that appear in a given text file and incrementing a counter which will then be used to set the size of the array. Here is what I have so far:

i = 0;
    int val;
   
    for(i=0; fscanf(dFile, "%1d", &val ) == 1; i++)
        printf(" val: %d\n", val);
    printf("i is: %d\n", i);

     //Dynamic Array
    // Defining a dynamic array of n ints:
    int n = i; 
    int *numbers = malloc(n * sizeof(int));

    //numbers[n];
    i = 0;
    //int numbers[30];
    readNumbers(numbers, i);

Not sure what I'm missing and I can't seem to find anything online that would help me figure it out. Any help/input would be greatly appreciated!

Recommended Answers

All 7 Replies

Here type casting of pointer is required

int *numbers = (int *)malloc(n * sizeof(int));

I have tried that and I just did again it's still not working the wayits supposed to. After that bit of code I copied earlier readNumbers method is called which should then iterate through the file again and write each int to the array but its not. It worked fine before when I manually declared the size of the array. Here is my full code it may help seeing the whole thing:

#include <stdlib.h>
#include <stdio.h>
#include <iostream>


FILE *dFile;



void LargeNumber(int numbers[], int k){
    int len = k;
    int i = 0;
    int big_one = numbers[0];
    int big_two = numbers[0];
    //Iterating through array and getting largest number
    for(i=0; i<len; i++) {
        if(numbers[i] > big_one){
            big_one = numbers[i];
        }}
    i = 0;
    for(i=0; i<len; i++) {
        if(numbers[i] > big_two){
            if(numbers[i] < big_one)
                big_two = numbers[i];
        }
    }
    printf("The largest number is: %d\n", big_one);
    printf("The second largest number is: %d\n", big_two);

}

//Calculates the average of the values in the array. Has two parameters:
//***the array
//***the number n of integers in the file (which was read by main() and it must
// return value the average as a float value
float Average(int numbers[], int i, int j){
    int total;
    float average;
    j=0;
    total = 0;
    //Adding up all the numbers in the Array.
    while(j < i){
        total = total + numbers[j];
        j++;
    }
    average = ((float)total)/i;
    printf("The average is: %f\n", average);
    LargeNumber(numbers,i);
}

//Reads the n integers from the input file. Has three parameters:
//***The Array
//***The input file(already opened in main())
//***The number n of integers in the file (which was already read by main
void readNumbers(int numbers[], int i){
    int j = 0;
    //Scanning file and storing ints to an array
    while(fscanf(dFile, "%1d", &numbers[i])!= EOF) {
        i++;
    }
    printf("Number of numbers read: %d\n\n", i);
    printf("The numbers are:\n");
    //Printing the numbers in the array
    for(j=0 ; j<i ; j++) { /* print numbers 1 by 1 */
        printf("%d\n", numbers[j]);
    }
    Average(numbers, i, j);
    
}



//Operations of the Main function
//***Open the input file
//***Reads the first line
//***Calls the functions readNumbers, Average, and LargeNumbers
//***It then prints the average, largest, and second largest values returned
//from the Average and LargeNumbers function
int main(int argc, char** argv) {
    int i,j,k;
    //Opening data file
    dFile = fopen("data.txt", "r");
    if(dFile==NULL) {
        printf("Error: can't open file.\n");
        return 1;
    }
    else {
        printf("File opened successfully.\n");
    }
    
    i = 0;
    int val;
    //while ( while(fscanf(dFile, "%1d", &val ) == 1, fscanf(dFile, "%1d", &val ) != 1 ), fscanf(dFile, "%1d")!= EOF, i++)
    for(i=0; fscanf(dFile, "%1d", &val ) == 1; i++)
        printf(" val: %d\n", val);
    printf("i is: %d\n", i);
     //Dynamic Array
    // Defining a dynamic array of n ints:
    int n = i;
   // int *numbers;
    int *numbers = (int *)malloc(n * sizeof(int));
    //numbers[n];
    i = 0;
    //int numbers[30];
    readNumbers(numbers, i);
    fclose(dFile);
}

I checked ur program its k..
but have to rewind the file pointer before u use it again because its already in EOF.

void readNumbers(int numbers[], int i){
..
rewind(dFile);
while(fscanf(dFile, "%1d", &numbers[i]) != EOF) {
  i++;
}
..

mark as solved if solved..

True, removing the redundant #include <iostream> and making sure to invoke the C compiler would be a lot simpler than going the other way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.