I am trying to complete an assignment, but first need to get the program to run. I get a debug error, "numberArray being used without being initialized. Am I'm missing something? I am using three functions. one to get the highest number in an array, second to get the lowest and passed by pointer notation, and third to change letters to capital.

// INCLUDES AND NAMESPACES

#include <iostream>

#include <iomanip>

using namespace std;

 

// DEFINES

#define ARRAY_SIZE 20

#define STRING_SIZE 20


// FUNCTION PROTOTYPES

int ReturnLargest(int);

int ReturnSmallest(int*);

int ToUpperCase(char*);

 

// GLOBALS

int highest;
int lowest;
int z;

 

// MAIN

int main() {
	int numberArray[20];
	char myGreeting[] = "Hello Merry Christmas"; 


	int ReturnLargest(numberArray[20]);
	int* ReturnSmallest(numberArray);
	int ToUpperCase(myGreeting[20]);
	cout << highest << endl;
	cout << lowest << endl;
	cout << z << endl;


 return 0;

}

 

// FUNCTION IMPLEMENTATIONS



int ReturnLargest(int numberArray[20]) {

	int highest;
	int i;
	int n;
	int max = numberArray[0];

	for(i = 0; i < ARRAY_SIZE; i++) {
		if(numberArray[i+1] > numberArray[i]) {
			highest = numberArray[i+1];
		}
		else {
			highest = numberArray[i];
		}
	}
	return highest;
}


int ReturnSmallest(int* numberArray) { 
	
	int lowest;
	int i;
	int n;
	int minimum = numberArray[0];

	for(i = 0; i < ARRAY_SIZE; i++) {
		if(numberArray[i+1] < numberArray[i]) {
			lowest = numberArray[i+1];
		}
		else {
			lowest = numberArray[i];
		}
	}
	return lowest;
}

		
int ToUpperCase(char* myGreeting) { 

	char tempArray[STRING_SIZE] = "";										// make an empty holder string

	int i = 0;																		// counter used to iterate through the string

	int j = 0;																		// counter used to iterate through the string

	//int z = 0;																		// counter used to iterate through the temp string

 

 // read letters from the end of reverseMe and put them

 // in the temporary array until the string is empty

	z = 0;

	while(myGreeting[i] != '\0') {

		j = 0;

	// find the last character in reverseMe

		while(myGreeting[j + 1] != '\0')

			j++;

 

	// copy that character into the temp array and terminate tempArray

			tempArray[z++] = myGreeting[j];

			tempArray[z] = '\0';

 

	// terminate reverseMe on the last character

			myGreeting[j] = '\0';

	}

 

 // tempArray now contains the reversed string

 // copy the contents of tempArray back into reverseMe

	i = 0;

	while(tempArray[i] != '\0') {

		myGreeting[i] = tempArray[i];

		i++;

	}

	myGreeting[i] = '\0';

	return z;
}

Recommended Answers

All 5 Replies

Line 39 declares numberArray, but doesn't initialize it to any meaningful value.

Line 43 sends numberArray to function (or tries to, leave off the [20], numberArray is the array, numberArray[20] is the 21st element of numberArray, but 20 as an index to numberArray is out of bounds).

Line 39 declares numberArray, but doesn't initialize it to any meaningful value.

Line 43 sends numberArray to function (or tries to, leave off the [20], numberArray is the array, numberArray[20] is the 21st element of numberArray, but 20 as an index to numberArray is out of bounds).

I take off the [20], but get this error: 2 IntelliSense: a value of type "int *" cannot be used to initialize an entity of type "int" c:\users\jones\documents\visual studio 2010\projects\assignment4\assignment4\assignment4.cpp 43

line 20 should be (int*) so you can pass numberArray not a single element of numberArray (which would be an individual int).

line 20 should be (int*) so you can pass numberArray not a single element of numberArray (which would be an individual int).

Thanks for your help, I've manage to get the program to run with your help. However, the first two functions doesn't give me the correct info. Any ideas you can give me?

// INCLUDES AND NAMESPACES

#include <iostream>

#include <iomanip>

using namespace std;

 

// DEFINES

#define ARRAY_SIZE 20

#define STRING_SIZE 20


// FUNCTION PROTOTYPES

int ReturnLargest(int []);

int ReturnSmallest(int []);

int ToUpperCase(char []);

 

// GLOBALS

int highest;
int lowest;
int z;

 

// MAIN

int main() {
	int numberArray[ARRAY_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
	char myGreeting[STRING_SIZE] = "HelloMerryChristmas"; 


	ReturnLargest(numberArray);
	ReturnSmallest(numberArray);
	ToUpperCase(myGreeting);
	cout << highest << endl;
	cout << lowest << endl;
	cout << z << endl;


 return 0;

}

 

// FUNCTION IMPLEMENTATIONS



int ReturnLargest(int numberArray[20]) {

	int highest = numberArray[0];
	int i;

	for(i = 0; i < ARRAY_SIZE; i++) {
		if(numberArray[i+1] > highest) {
			highest = numberArray[i+1];
		}
		else {
			highest = numberArray[i];
		}
	}
	return highest;
}


int ReturnSmallest(int* numberArray) { 
	
	int lowest = numberArray[0];
	int i;

	for(i = 0; i < ARRAY_SIZE; i++) {
		if(numberArray[i+1] < lowest) {
			lowest = numberArray[i+1];
		}
		else {
			lowest = numberArray[i];
		}
	}
	return lowest;
}

		
int ToUpperCase(char* myGreeting) { 

	char tempArray[STRING_SIZE] = "";										// make an empty holder string

	int i = 0;																		// counter used to iterate through the string

	int j = 0;																		// counter used to iterate through the string

	//int z = 0;																		// counter used to iterate through the temp string

 

 // read letters from the end of myGreeting and put them

 // in the temporary array until the string is empty

	z = 0;

	while(myGreeting[i] != '\0') {

		j = 0;

	// find the last character in reverseMe

		while(myGreeting[j + 1] != '\0')

			j++;

 

	// copy that character into the temp array and terminate tempArray

			tempArray[z++] = myGreeting[j];

			tempArray[z] = '\0';

 

	// terminate reverseMe on the last character

			myGreeting[j] = '\0';

	}

 

 // tempArray now contains the reversed string

 // copy the contents of tempArray back into reverseMe

	i = 0;

	while(tempArray[i] != '\0') {

		myGreeting[i] = tempArray[i];

		i++;

	}

	myGreeting[i] = '\0';

	return z;
}

You are overreading the array indexes using the indexes of i + 1. Start the loop at i = 1 instead of 0 and change so it doesn't try to access memory the array doesn't have.

NOTE: array will be beyond the array bounds when i = ARRAY_SIZE - 1 beacause ARRAY_SIZE - 1 + 1 will be ARRAY_SIZE and array[ARRAY_SIZE] is out of bounds.

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.