Im not used to using pointers or coding in C. I've created a void function in which I defined a couple arrays and populated them using my Input parameters. Now I need to use these arrays in my main but unsure on how to call them. Following is my function code :
void showCDF(int Integer_Array[], int length){ //Calculates the CDF and stores it in "y_values"
int i = 1; // i initialized at 1
int y_axis[20];
int size_of_x = Integer_Array[length-1]- Integer_Array[0]+1; // size of x is the difference between the first and
int x_axis[size_of_x]; // last element of input array.
x_axis[0] = Integer_Array[0]; // first element of x is the first element of input array
//printf("%d ", x_axis[0]); // print out the first element
while (x_axis[i-1]<Integer_Array[length-1]){ // while previous element of x < last element of Input
x_axis[i]= x_axis[i-1] + 1; // adding the previous value of x to 1 store as this value
//printf("%d ", x_axis[i]); // print out all consecutive elements of x
i +=1;
}
printf("\n");
for (i=0;i<21;i++){
y_axis[i] = 5*i;
//printf("%d \n",y_axis[i]);
}
int y_values[size_of_x + 1]; // size of y is dependant on the size of x
for (int j=0; j < size_of_x +1 ; j++){
float n = 0; // Count to show total, initialized at 0
int i= 0;
while (x_axis[j] >= Integer_Array[i] && i<length){ // While x at j is greater than or equal to values of
n ++; // Integer_array at i, AND i is less than length.
i ++; // Then increase the count and i
} // else:
y_values[j]=(n/length)*100; // Then corresponding y at j= % of n / length (or the cdf)
//printf( "%d ", y_values[j]); // print out the cdf
}
}
In my main Im calling my function and variables y_axis, y_value, x_axis like this:
int y_axis[20];
showCDF(integerArray, NUMBER_ENTRIES);
for (i=0;i<21;i++){
printf("%d \n", y_axis[i]);
}
But this just gives me a gibberish number like:
0
0
0
0
0
0
0
0
0
0
0
0
1
0
4199677
0
0
0
0
0
825307441
Any help would be appreciated!
Related
I input 4566371 and find out that uninitialized array elements are not 0 as said in our texts
#include <stdio.h>
#define MAX 10
// Function to print the digit of
// number N
void printDigit(long long int N)
{
// To store the digit
// of the number N
int array_unsorted[MAX];//array_unsorteday for storing the digits
int i = 0;//initializing the loop for the first element of array_unsorted[]
int j, r;
// Till N becomes 0,we will MOD the Number N
while (N != 0) {
// Extract the right-most digit of N
r = N % 10;
// Put the digit in array_unsorted's i th element
array_unsorted[i] = r;
i++;
// Update N to N/10 to extract
// next last digit
N = N / 10;
}
// Print the digit of N by traversing
// array_unsorted[] reverse
for (j =MAX; j >=0; j--)
{
printf("%d ", array_unsorted[j]);
}
}
// Driver Code
int main()
{
long long int N;
printf("Enter your number:");
scanf("%lld",&N);
printDigit(N);
return 0;
}
output:
Enter your number:4566371
77 0 8 32765 4 5 6 6 3 7 1
Process returned 0 (0x0) execution time : 2.406 s
Press any key to continue.
The other values should be o right?Why 77,0,32765 this way?Why not all are 0?like 0 0 0 0 4 5 6 6 3 7 1?
An array of integers declared inside a function has indeterminate values if it is uninitialized. If a similar array is declared at global scope, outside all functions, it will be initialized with zeros by default.
To make an array that is always initialized to zeros, do this:
int array_unsorted[MAX] = {0};
This works because in C, = {0} will initialize all values with zero. If you say = {10, 20} it will initialize the first two elements as written, and the rest to zero.
You have correctly reserved a memory space for your array array_unsorted but the memory has not been cleaned before use. This memory reserved it was probably used by another function or variable before yours! This is why it already has some values in it. You should set it to 0 manually before starting to use it.
As noted in other answers, you may want to initialize your integer array. Whether or not you do that, you might want to replace the for loop statement conditions of:
for (j =MAX; j >=0; j--)
with:
for (j = i - 1; j >=0; j--) /* Start at the place the digit storage ended */
That way, you are not moving into array elements that were not used in the digit storage portion of your function.
Regards.
I have this following program which calculates the determinant of an NxN matrix, the program itself works, but when I try to calculate the 2nd order matrix determinant at the end of recursion, accessing the 2d 2x2 array elements gives me wrong values.
I am using recursion to be able to calculate the determinant of higher-order matrices, findDet() is the recursive function, that uses a loop to call itself for each element of the first row in a given matrix and sub-matrices if their order is greater than two.
/******************************************************************************
Program to find the determinant a matrix
*******************************************************************************/
#include <stdio.h>
#define N 3
long int findDet(int arr[][N], size_t order); // To find the determinant
void initNewArr(int newArr[][N-1], int prevArr[][N], size_t order, int exCol); // To create sub matrices
int main()
{
int order = N;
printf("This program is to find determinant of a given matrix.\n\
\rNumber of row and column is going to be equal.\n");
// printf("Enter the order of the matrix: ");
// scanf("%d", &order);
// Read the input matrix from the user
int matrix[order][order];
printf("Enter matrix elements...\n");
for(int i = 0; i < order; i++) {
for(int j = 0; j < order; j++) {
printf("Enter matrix[%d][%d]: ", i+1, j+1);
scanf("%d", &matrix[i][j]);
}
}
// Print the matrix
printf("New Matrix itself...\n");
for(int i = 0; i < order; i++) {
for(int j = 0; j < order; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
// Calling findDet() to calculate determinant and store it in "det"
long int det = findDet(matrix, order);
// Print the determinant
printf("\nDeterminant of the matrix = %li", det);
return 0;
}
long int findDet(int arr[][N], size_t order) {
// if order is 2 calculate the determinant of 2nd order matrix
if(order == 2) {
int detS = ((arr[0][0]) * (arr[1][1])) - ((arr[0][1]) * (arr[1][0]));
/*
This following expression shows that accessed values are not correct
for this reason, the calculated determinant of 2nd order matrix is
not correct.
The whole problem is in this block of code
*/
printf("=== arr[0][0] * arr[1][1] - arr[0][1] * arr[1][0] = %d * %d - %d * %d\n", arr[0][0], arr[1][1], arr[0][1], arr[1][0]);
printf("Result of above expression === %d\n", detS);
return detS;
} // if order is higher then continue to break down matrix to 2nd order step by step
long int det = 0;
/*
The following for loop is for multiplying each element of
first row by determinant of sub-matrix,
which in case of below 3x3 matrix:
2 -3 1
2 0 -1 its determinant = 2 * (det of sub-matrix1) - (-3) * (det of sub-matrix2) + 1 * (det of sub-matrix3)
1 4 5
sub-matrices are:
>>> sub-matrix1
0 -1
4 5 --> in the first iteration of the array inside findDet()
while accessing the
arr[0][0] gives value 0
arr[0][1] gives value -1
arr[1][0] gives value 5
arr[1][1] gives value 1
>>> sub-matrix2
2 -1
1 5 --> in the second iteration of the array inside findDet()
while accessing the
arr[0][0] gives value 2
arr[0][1] gives value -1
arr[1][0] gives value 5
arr[1][1] gives value 1
>>> sub-matrix3
2 0
1 4 --> in the third iteration of the array inside findDet()
while accessing the
arr[0][0] gives value 2
arr[0][1] gives value 0
arr[1][0] gives value 4
arr[1][1] gives value 1
But since we get wrong values for sub-matrices the final determinant is not correct
*/
for(int i = 0; i < order; i++) {
// New sub matrix in each iteration
int newArr[order-1][order-1];
initNewArr(newArr, arr, order, i);
// Print the sub matrix
printf("\nNewly Created Sub Matrix itself...\n");
for(int i = 0; i < order-1; i++) {
for(int j = 0; j < order-1; j++) {
printf("%d\t", newArr[i][j]);
}
printf("\n");
}
// Calculate Determinant
if(i % 2 == 0) { // if i is 0 or even which then becomes odd-th element of matrix add result to det
det += (arr[0][i] * findDet(newArr, order-1));
} else { // otherwise subtract the result from the det
det -= (arr[0][i] * findDet(newArr, order-1));
}
}
return det;
} // ================== findDet()
void initNewArr(int newArr[][N-1], int prevArr[][N], size_t order, int exCol) {
for(int i = 0; i < order; i++) {
for(int j = 0; j < order; j++) {
if(i != 0 && j != exCol) { // When element is not in first row and exCol(excluded column) assign it to sub matrix
newArr[i-1][j > exCol ? j-1 : j] = prevArr[i][j]; // When column is greater than exCol subtract 1 from it
}
}
}
}
The recursion works perfectly and the problem is in the expression denoted above.
edit
for example if I input
2 -3 1
2 0 -1
1 4 5
while accessing the arr[0][0], arr[0][1], arr[1][0] and arr[1][1] in following sub-matrices
0 -1
4 5 --> in the first iteration of the array inside findDet()
2 -1
1 5 --> in the second iteration of the array inside findDet()
2 0
1 4 --> in the third iteration of the array inside findDet()
don't give me all the original values, they give some random values and some values that exist in the matrix.
you can check code comments for more details, I know this question is long and a bit confusing but I wish some of you can help me?
Can you please help me find the problem?
Thanks
given a set of 10 digit number i need to find how many times the index appears in the give number e.g
given 2 2 0 0 0 3 0 0 1 0 as an array element the array index from 0-9 so i have something like
0 1 2 3 4 5 6 7 8 9
2 2 0 0 0 3 0 0 1 0 ---> input
00115558 -----> output
the first index 0 appear twice, the second index 1 appear twice, the third index 0 appear 0 times so it's ignore and so on so the outcome is 00115558 and now i need to rearrange the output such that 0 must not start so i need something like 10015558
i was able to get the initial output like 00115558, but i need to save it into an array so i can loop through the array and check for value greater than 0 and swap the position.
int main() {
int i,j,len;
int array[10],output[10];
printf("Enter 10 digit number separated by space: \n");
for (i = 0; i < 10; i++)
scanf("%d", &array[i]);
len = sizeof(array)/sizeof(array[0]);
for(i=0; i<len; i++)
{
if(array[i] != 0)
{
for(j=0; j<array[i]; j++)
//output[j] = i;
printf("%d ", i);
}
}
I tried to save the output into another array and i printed the array outside the loop to see the content but the result wasn't what i wanted. How can i save my value into the output[] array. thanks for any help
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,len;
int array[10],output[10];
printf("Enter 10 digit number separated by space: \n");
for (i = 0; i < 10; i++)
scanf("%d", &array[i]);
len = sizeof(array)/sizeof(array[0]);
for(i=0; i<len; i++)
{
if(array[i] != 0)
{
for(j=0; j<array[i]; j++)
//output[j] = i;
printf("%d ", i);
}
}
}
Sir, i tried you exact code as above in "Dev c++". I get answer is 00115558. I think your Code is absolute Right. change your IDE software or Update that.
try to input 20 numbers with array and to output
the numbers in the double location only but somehow it's print
also the 0 location... please help.
#include<stdio.h>
#define n 20
int main()
{
int num[n]={0},i=0,order=1,double_locaion=0;
for(i=0;i<n;i++)
{
printf("please enter %d number\n",order);
scanf("%d",&num[i]);
order++;
}
for(i=0;i<n;i++)
{
if (i%2==0 && i!=1 && i!=0)
{
printf("%d\n",num[i]);
}
}
}
Try this, start with 2 and increase by 2 every time, the you don't have to deal with 0th element and odd element.
for (i = 2; i < n; i += 2)
{
printf("%d\n",num[i]);
}
First of all there is no way that your code is printing the 0-th location of the array. That's impossible given the condition of the if statement.
Secondly n- you don;t need to use macro expansion for that name.
/* This program takes 20 integer number from input.
* Prints the numbers entered in odd positions.(First,Third,..etc).
*/
#include<stdio.h>
#include<stdlib.h>
#define NUM 20
int main(void)
{
int numArr[NUM];
for(size_t i = 0; i < NUM; i++) {
printf("please enter %zu number\n",i+1);
if( scanf("%d",&numArr[i]) != 1){
fprintf(stderr, "%s\n","Error in input" );
exit(1);
}
}
for(size_t i = 0; i < n; i++)
{
if( i%2 == 0 )// if you want to omit the first number put the
// the condition (i%2 == 0 && i)
{
printf("%d\n",numArr[i]);
}
}
return 0;
}
What you did wrong that your code skipped 0th element?
if (i%2==0 && i!=1 && i!=0)
^^^^
i when 0 makes this condition false - and you never get to print it.
i!=1 ?
If i=1 then i%2 will be 1, so you will not even check the second conditions, the whole conditional expression will become false. So you can safely omit this logic.
Is there a better way?
Sure,
for(size_t i = 0; i < n; i += 2){
printf("%d\n",num[i]);
}
Explanation
If you consider that every time you check the modular arithmetic of 2 the elements which results to 0 remained are
0,2,4,6,8,10,...18
See the pattern? Starts with 0 and increments by 2 each time and when does it stop? Yes before reaching 20 coding it we get
for(size_t i = 0; i < n; i += 2){
/* Initialize with i=0 as first number is 0 (i=0)
* Increments by 2 (i+=2)
* Runs when less than 20 (i<n)
*/
printf("%d\n",num[i]);
}
If you want to omit the 0-th index do initialize properly
for(size_t i = 2; i < n; i += 2){
If you mean you want the numbers from array that are present at even position than you can do like this:
for (i = 2; i < n; i=i + 2) //Initialize i = 0 if 0 is consider as even
{
printf("%d\n",arr[i]);
}
I above code i is initialized to 2 and the increment in each iteration is 2 so it will access elements only at even position (2,4,6...).
I have written a small piece of code that would perform Run length encoding kind of stuff on 1-D array but still far from desired result.
main()
{
int a[8]={2,0,0,0,3,0,0,9};
int i,temp,ct=0,flag,m;
int found[90]={0};
for(i=0;i<=7;i++)
{
if(!a[i])
{
ct++;
if(!found[a[i]])
{
flag=i;
found[a[i]]=1;
}
}
}
a[flag]=ct;
m=ct;
for(i=0;i<m;i++)
{
printf("%d",a[i]);
}
}/* end of main*/
Now for above array i would like to have output something below
2 5 0 3 9
But with my piece of code am getting
2 5 0 0 3
Can I have any suggestion on that?
Shouldn't run length encoding turn 2,0,0,0,3,0,0,9 into 2 1 0 3 3 1 2 0 9 1?
1) The first thing I see is wrong is that you aren't looking at the entire array. You're using < to stop before 8, but also stopping at 7, so you only evaluate array items 0 - 6.
2) If ct stands for count it's never reset (ct=0 only on declaration). Also it's assignment is this: a[flag]= ct; which overwrites your original data. It basically tracks the value of i.
This is my version I've just put together:
#define SZ 8
main()
{
int a[SZ]={2,0,0,0,3,0,0,9};
int i; //absolute position
int runningCount = 1; //because we start at array index 1 and not zero
for (i = 1; i <= SZ; i++) {
if (a[i - 1] == a[i]) //value same as one before it...
runningCount++;
else { // new value found. print last one, and the count of the last one.
printf("%d %d ", a[i - 1], runningCount);
runningCount = 1; //reset for next loop
}
}
return 0;
}
The output is 2 1 0 3 3 1 0 2 9 1
Ok based on the comment left below, your algorithm would actually look like this:
#define SZ 8
main()
{
int a[SZ]={2,0,0,0,3,0,0,9};
int i; //absolute position
int zero_count = 0; //target zeros specifically...
for (i = 0; i < SZ; i++) {
if (a[i] == 0)
zero_count++;
}
//now write it out in a bizarre, unparsable format again...
for (i = 0; i < SZ; i++) {
if (a[i] != 0) //write out all non zero values
printf("%d ", a[i]);
if (i == 0) { //this says put the zero count after the first number was printed
printf("%d 0 ", zero_count); //inserting it into a strange place in the array
}
}
return 0;
}
which outputs: 2 5 0 3 9
You need a <= in your for loop:
for(i=0;i<=7;i++)
instead of
for(i=0;i< 7;i++)
Otherwise you miss the last element.
All you appear to be doing is (a) counting the number of times 0 occurs in the array, and (b) replacing the first occurrence of 0 with that count. It's not clear how this is meant to be a useful encoding.
In any case, you're not getting your desired result, at least in part, because you're only modifying one element of the array. I suspect what you want, or at least think you want, is to shift the non-zero elements of the array to the left as you encounter them.
What is the utility of compressing the array in the way you propose? Is some other piece of code going to have to reconstruct the original, and if so how do you expect to do so from your desired result?