My code compiles in CodeBlocks but I am getting warnings concering the countZero
ID: 3711395 • Letter: M
Question
My code compiles in CodeBlocks but I am getting warnings concering the countZerosOnes function and the pointers.
I cannot figure out why or how to fix it?
#include
#include
#define MAX 100
// Displays array
void display (int data[MAX], int size);
// Reverses order of array
void reverse (int data[MAX], int size);
// Counts number of zeros and ones in array
int countZerosOnes (int data[MAX], int size, int *pNumZeros, int *pNumOnes);
int main (void) {
FILE *fp;
int size = 0; // Size of array
int numZeros; // Number of zeros in array
int numOnes; // Number of ones in array
int totalCount; // Number of zeros and ones in array
int data[MAX]; // Array
int i;
int *pNumOnes;
int *pNumZeros;
// Part 1: Opens file
fp = fopen("data.txt", "r");
if(fp == NULL)
{
printf("File does not exist! ");
exit(-1);
}
// Loops to assign values into array and find the size
for(i=0; fscanf(fp, "%d ", &size) != EOF; i++)
{
data[i] = size;
size = i + 1;
}
// Part 2: Display the array
printf("Original Data: ");
display (data, size);
// Part 3: Reverse the array
reverse (data, size);
// Part 4: Count zeros and ones
countZerosOnes (data, size, &pNumZeros, &pNumOnes);
totalCount = countZerosOnes (data, size, &pNumZeros, &pNumOnes);
printf(" Number of zeros in original array = %d", pNumZeros);
printf(" Number of ones in original array = %d", pNumOnes);
printf(" Total count of ones & zeros = %d ", totalCount);
return 0;
}
// Code Part 2: Function to display array
void display (int data[MAX], int size)
{
int j = 0;
printf("==> ");
for(j=0; j {
printf("%d ", data[j]);
}
}
// Code for Part 3: Function to reverse array
void reverse (int data[MAX], int size)
{
int temp;
int start;
int end = size - 1;
// Loops until the array has been flipped
for(start=0; start<(size/2); start++, end--)
{
temp = data[start]; // Temp holds original data
data[start] = data[end]; // Flips array
data[end] = temp; // Original data given to end
}
printf(" Flipped Data: ");
display(data,size);
}
// Code for Part 4: Function to count zeros and ones
countZerosOnes (int data[MAX], int size, int *pNumZeros, int *pNumOnes)
{
int i;
int numZeros = 0;
int numOnes = 0;
// Loops to find number of zeros in array
for(i=0; i {
if(data[i] == 0)
numZeros++;
}
//Loops to find number of ones in array
for(i=0;i {
if(data[i] == 1)
numOnes++;
}
*pNumZeros = numZeros;
*pNumOnes = numOnes;
return(numZeros + numOnes);
}
Explanation / Answer
Fixed your code. Removed the *pNumOnes and *pNumZeros. You only need to pass the address of the variable from main(), so the receiving function will have it as a pointer. I have solved the same question earlier and know the question completely. So fixed the code accordingly. Please do rate the answer if it helped. Thank you.
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
// Displays array
void display (int data[MAX], int size);
// Reverses order of array
void reverse (int data[MAX], int size);
// Counts number of zeros and ones in array
int countZerosOnes (int data[MAX], int size, int *pNumZeros, int *pNumOnes);
int main (void) {
FILE *fp;
int size = 0; // Size of array
int numZeros; // Number of zeros in array
int numOnes; // Number of ones in array
int totalCount; // Number of zeros and ones in array
int data[MAX]; // Array
int i;
// Part 1: Opens file
fp = fopen("data.txt", "r");
if(fp == NULL)
{
printf("File does not exist! ");
exit(-1);
}
// Loops to assign values into array and find the size
for(i=0; fscanf(fp, "%d ", &size) != EOF; i++)
{
data[i] = size;
size = i + 1;
}
// Part 2: Display the array
printf("Original Data: ");
display (data, size);
// Part 3: Reverse the array
reverse (data, size);
// Part 4: Count zeros and ones
totalCount = countZerosOnes (data, size, &numZeros, &numOnes);
printf(" Number of zeros in original array = %d", numZeros);
printf(" Number of ones in original array = %d", numOnes);
printf(" Total count of ones & zeros = %d ", totalCount);
return 0;
}
// Code Part 2: Function to display array
void display (int data[MAX], int size)
{
int j = 0;
printf("==> ");
for(j=0; j < size; j++){
printf("%d ", data[j]);
}
}
// Code for Part 3: Function to reverse array
void reverse (int data[MAX], int size)
{
int temp;
int start;
int end = size - 1;
// Loops until the array has been flipped
for(start=0; start<(size/2); start++, end--)
{
temp = data[start]; // Temp holds original data
data[start] = data[end]; // Flips array
data[end] = temp; // Original data given to end
}
printf(" Flipped Data: ");
display(data,size);
}
// Code for Part 4: Function to count zeros and ones
int countZerosOnes (int data[MAX], int size, int *pNumZeros, int *pNumOnes)
{
int i;
int numZeros = 0;
int numOnes = 0;
// Loops to find number of zeros in array
for(i=0; i < size; i++){
if(data[i] == 0)
numZeros++;
}
//Loops to find number of ones in array
for(i=0;i<size; i++){
if(data[i] == 1)
numOnes++;
}
*pNumZeros = numZeros;
*pNumOnes = numOnes;
return(numZeros + numOnes);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.