Joseph  Norton

Joseph Norton

1548231398

Increased array size. after increaseSize function some arguments of the same array are invalid. C lang

The program takes some txt input from "numbers.txt" file. It first counts the amount of all the numbers (freqCount)in that text file, than reads the file again and with the use of malloc it creates two arrays A and B , of which both have size equal to the amount of all the numbers in the text file. So far so good.

Now i want to increase the size of the array A, so that I can put "freqCount" more arguments in it. In the freqRepeat function I created, there is a function increaseSize which takes that same array A, and uses realloc to add 2*freqCount more arguments in it.

After calling the mentioned function increaseSize there is a problem, because only part of the arguments remain unchanged, and there are few arguments that become some huge number. This is a major issue. Can anyone please provide me with some help ? thanks

ps. I include the expemplary text file input at the end of the code.

#include <stdio.h>
#include <stdlib.h>
int read_ints(const char *file_name, int *result);
int *scanFreq(const char *file_name, int *A, int *B, int *resultTab);
int freqRepeat(int *A, int *B, int freqCount);
int *increaseSize(int *A, int freqCount);
void calcmalc(int freqCount);
int *nextArray(int *A, int *B, int freqCount, int freqStart);

int main()
{
int result = 0;
int resultTab = 0;
int freqCount;
freqCount = read_ints(“numbers.txt”, &result);
printf(“freqCount is %d”, freqCount);
int *A = (int *)malloc(freqCount * sizeof(int));
int *B = (int *)malloc(freqCount * sizeof(int));
scanFreq(“numbers.txt”, A, B, &resultTab);
freqRepeat(A, B, freqCount);

}
int read_ints(const char *file_name, int *result)
{
FILE *file = fopen(“numbers.txt”, “r”);
int i = 0;
int n = 0; //row number//

if (file == NULL)
{
printf(“unable to open file %s”, file_name);
}

while (fscanf(file, “%d”, &i) == 1)
{
n++;
printf(“%d\n “, i);
*result += i;
printf(”\n we are at row nr. %d sum of this number and all numbers before is: %d\n”, n, *result);
}
fclose(file);
return n;
}
int *scanFreq(const char *file_name, int *A, int *B, int *resultTab)
{
FILE *file = fopen(“numbers.txt”, “r”);
int i = 0;
int n = 0; //row number//

if (file == NULL)
{
printf(“unable to open file %s”, file_name);
}

while (fscanf(file, “%d”, &i) == 1)
{
n++;
*resultTab += i;
B[n] = i;
A[n] = *resultTab;
}
fclose(file);
return 0;
}

int freqRepeat(int *A, int *B, int freqCount)
{
int lastFrequency;

lastFrequency = freqCount;
freqCount = freqCount + freqCount;
A = increaseSize(A, freqCount);

printf(“\n\nwcis enter\n\n”);
getchar();

for (int i = 1; i < 15; i++)
{
printf(“array argument after increasing array size %d \n”, A[i]);

// why some of the arguments have been changed ????????
}

return 0;
}
int *increaseSize(int *A, int freqCount)
{

return realloc(A, 2 * sizeof(int));
}

text input:
-14
+15
+9
+19
+18
+14
+14
-18
+15
+4
-18
-20
-2
+17
+16
-7
-3
+5
+1
-5
-11
-1
-6
-20
+1
+1
+4
+18
+5
-20
-10
+18
+5
-4
-5
-18
+9
+6
+1
-19
+13
+10
-22
-11
-14
-17
-10
-1
-13
+6
-17


#c #arrays

What is GEEK

Buddha Community

Daisy Rees

1548321421

You unconditionally resize your array to only contain two int elements. Always. Any access to elements beyond those two will lead to undefined behavior.

You probably meant to do

return realloc(A, freqCount * sizeof(int));

Joseph  Norton

Joseph Norton

1548231398

Increased array size. after increaseSize function some arguments of the same array are invalid. C lang

The program takes some txt input from "numbers.txt" file. It first counts the amount of all the numbers (freqCount)in that text file, than reads the file again and with the use of malloc it creates two arrays A and B , of which both have size equal to the amount of all the numbers in the text file. So far so good.

Now i want to increase the size of the array A, so that I can put "freqCount" more arguments in it. In the freqRepeat function I created, there is a function increaseSize which takes that same array A, and uses realloc to add 2*freqCount more arguments in it.

After calling the mentioned function increaseSize there is a problem, because only part of the arguments remain unchanged, and there are few arguments that become some huge number. This is a major issue. Can anyone please provide me with some help ? thanks

ps. I include the expemplary text file input at the end of the code.

#include <stdio.h>
#include <stdlib.h>
int read_ints(const char *file_name, int *result);
int *scanFreq(const char *file_name, int *A, int *B, int *resultTab);
int freqRepeat(int *A, int *B, int freqCount);
int *increaseSize(int *A, int freqCount);
void calcmalc(int freqCount);
int *nextArray(int *A, int *B, int freqCount, int freqStart);

int main()
{
int result = 0;
int resultTab = 0;
int freqCount;
freqCount = read_ints(“numbers.txt”, &result);
printf(“freqCount is %d”, freqCount);
int *A = (int *)malloc(freqCount * sizeof(int));
int *B = (int *)malloc(freqCount * sizeof(int));
scanFreq(“numbers.txt”, A, B, &resultTab);
freqRepeat(A, B, freqCount);

}
int read_ints(const char *file_name, int *result)
{
FILE *file = fopen(“numbers.txt”, “r”);
int i = 0;
int n = 0; //row number//

if (file == NULL)
{
printf(“unable to open file %s”, file_name);
}

while (fscanf(file, “%d”, &i) == 1)
{
n++;
printf(“%d\n “, i);
*result += i;
printf(”\n we are at row nr. %d sum of this number and all numbers before is: %d\n”, n, *result);
}
fclose(file);
return n;
}
int *scanFreq(const char *file_name, int *A, int *B, int *resultTab)
{
FILE *file = fopen(“numbers.txt”, “r”);
int i = 0;
int n = 0; //row number//

if (file == NULL)
{
printf(“unable to open file %s”, file_name);
}

while (fscanf(file, “%d”, &i) == 1)
{
n++;
*resultTab += i;
B[n] = i;
A[n] = *resultTab;
}
fclose(file);
return 0;
}

int freqRepeat(int *A, int *B, int freqCount)
{
int lastFrequency;

lastFrequency = freqCount;
freqCount = freqCount + freqCount;
A = increaseSize(A, freqCount);

printf(“\n\nwcis enter\n\n”);
getchar();

for (int i = 1; i < 15; i++)
{
printf(“array argument after increasing array size %d \n”, A[i]);

// why some of the arguments have been changed ????????
}

return 0;
}
int *increaseSize(int *A, int freqCount)
{

return realloc(A, 2 * sizeof(int));
}

text input:
-14
+15
+9
+19
+18
+14
+14
-18
+15
+4
-18
-20
-2
+17
+16
-7
-3
+5
+1
-5
-11
-1
-6
-20
+1
+1
+4
+18
+5
-20
-10
+18
+5
-4
-5
-18
+9
+6
+1
-19
+13
+10
-22
-11
-14
-17
-10
-1
-13
+6
-17


#c #arrays

Ari  Bogisich

Ari Bogisich

1590149040

C++ Arrays And Multidimensional Arrays

In this article we are going to talk about C++ Arrays And Multidimensional Arrays, so c++ arrays are collection of the elements that are the same type. if you remember our article on variables, when we want to store one value we are going to use variable. If we want to store two or three values we need to use more variables. but it is not a good way, by this reason we have arrays. also the elements of array are stored in the continues memory location.

#c++ #c++ tutorials #c #c#

Sadie  Ratke

Sadie Ratke

1590293760

C++ Arrays And Multidimensional Arrays

In this video we are going to talk about C++ Arrays And Multidimensional Arrays, so c++ arrays are collection of the elements that are the same type. if you remember our video on variables, when we want to store one value we are going to use variable. If we want to store two or three values we need to use more variables. but it is not a good way, by this reason we have arrays. also the elements of array are stored in the continues memory location.

#c #c# #c++ #programming-c

Ari  Bogisich

Ari Bogisich

1590565090

A Guide to using the strdup() function in C/C++

In this article, we’ll take a look at using the strdup() function in C/C++.

The strdup() function is very useful if you want to duplicate the contents of a string onto another string.

Let’s see how we can utilize this function, using some simple examples.

#c programming #c++ #c #c#

Tamale  Moses

Tamale Moses

1624240146

How to Run C/C++ in Sublime Text?

C and C++ are the most powerful programming language in the world. Most of the super fast and complex libraries and algorithms are written in C or C++. Most powerful Kernel programs are also written in C. So, there is no way to skip it.

In programming competitions, most programmers prefer to write code in C or C++. Tourist is considered the worlds top programming contestant of all ages who write code in C++.

During programming competitions, programmers prefer to use a lightweight editor to focus on coding and algorithm designing. VimSublime Text, and Notepad++ are the most common editors for us. Apart from the competition, many software developers and professionals love to use Sublime Text just because of its flexibility.

I have discussed the steps we need to complete in this blog post before running a C/C++ code in Sublime Text. We will take the inputs from an input file and print outputs to an output file without using freopen file related functions in C/C++.

#cpp #c #c-programming #sublimetext #c++ #c/c++