#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2

    Question About Error for NewNames List in C++


    Hello,

    So I followed an example in the Ivor Horton book on Beginner Visual C++, and this program pretty much attempts to look at a current list of names, and sees if it can find the name "Fred", which at first, will return a negative integer value from the BinarySearch() function.

    The first portion of the program should be ignored, because it was just an example of looking for a particular value within an integer array inside a CLR/CLI program using the Binary Search function.

    The last portion of the code involves a revision from the author that will go through various loops to make an entirely new string array and add 1 to its length so it can accomodate poor old Fred into the new string array.

    The issue I'm having is that when I attempt to output the array at the end of the program, I'm receiving a "A1".

    Is this some sort of representation of Fred's position in Hexadecimal (sounds really vague, and probably wrong, but I have no idea as to how this occurred)

    Anyway, thanks for reading my post!

    Here's my code:

    // Ex4_15.cpp : main project file.

    Code:
    #include "stdafx.h"
    
    using namespace System;
    
    int main(array<System::String ^> ^args)
    {
        // BinarySearch() function: This uses a binary search algorithm to find the index 
    	// position of a given element in the entire array or within a range of elements 
    	// inside a 1-Dimensional array
    
    	Console::WriteLine(L"This is the first portion of the program: ");
    
    	array<int>^ values = {23, 45, 68, 94, 123, 127, 150, 203, 299}; 
    
    	// This creates an integer array with 9 values
    	
    	int toBeFound = 127; 
    
    	// This creates a toBeFound variable containing the value 127 which we are looking for
    	// in the values array
    	
    	int position1 = Array::BinarySearch(values, toBeFound); 
    
    	// This position variable will utilize the BinarySearch function that will find the
    	// index value of 127 inside the values array by specifying the array it is looking
    	// through in its first argument: values, and what it wants to find in its second
    	// argument: toBeFound
    	
    	if(position1 < 0) 
    
    		// This will occur if position is not found aka, the BinarySearch function
    		// returns a negative value after not finding the specified value inside the 
    		// integer array, which will not occur because 127 is IN the value array at
    		// position 5
    
    		// If the element that we are looking for is not found, a negative integer value
    		// is given by default from the BinarySearch() function
    		
    		Console::WriteLine(L"{0} was not found.", toBeFound); 
    	
    	else 
    		
    		Console::WriteLine(L"{0} was found at index position {1}.", toBeFound, position1);
    
    		// This will just output the 127 found at elemental position 5
    
    
    	// ________________________
    	// NEW PART OF THE PROGRAM:
    	// ________________________
    
    	Console::WriteLine(L"Here is a new portion of the program: \n \n");
    
    	array<String^>^ names = {"Jill", "Ted", "Mary", "Eve", "Bill", "Al", "Ned", 
    							 "Zoe", "Dan", "Jean"}; 
    
    	// This will create a string array containing several names, NOT INCLUDING FRED
    
    	// I point that out because we will later try to find Fred within this list, but
    	// he wasn't listed in the first place so his result will return a negative integer
    	
    	array<int>^ weights = {103, 168, 128, 115, 180, 176, 209, 98, 190, 130}; 
    
    	// This will create an integer array for each individual specified in the names array
    	
    	array<String^>^ toBeFound2 = {"Bill", "Eve", "Al", "Fred"}; 
    
    	// This is a string array that contains the names of people we want to find
    
    	// I later renamed this to be toBeFound2 because of the previous use of the word
    	// toBeFound in the example above (caused about 5 errors, so at least this was
    	// the simple solution heh :D)
    	
    	Array::Sort(names, weights); 
    
    	// This will use the Sort() function from the Array Class to sort the names with
    	// their associated weights in alphabetical order based on their names
    	
    	int result = 0; 
    
    	// This creates a result variable that we will later use to store the result from
    	// the BinarySearch function that will look through each of the names in the names
    	// array for the names that we want to find
    	
    	for each(String^ name in toBeFound2){ 
    
    		// This ia for each loop that will look throughout each name in to be found
    		
    		result = Array::BinarySearch(names, name); 
    
    		// This will then compare the current element aka the current name to the actual
    		// names array and will give a positive or negative result
    
    		// Notice this is a loop, so we will get 3 positive values for Bill, Eve, and Al
    		// but a negative integer value for Fred
    		
    		if(result < 0)
    			
    			Console::WriteLine(L"{0} was not found.", name); 
    
    
    		// This will occur for Fred since his name is NOT found within the names array
    		// to begin with
    
    		// The value returned by BinarySearch when it looks for Fred is the bitwise 
    		// complement of the index position of the first element that is GREATER than 
    		// the object you are looking for, 
    		
    		// OR IN OTHER TERMS: the bitwise complement of the Length property of the array 
    		// if no element is greater than the object sought.
    
    		else
    			Console::WriteLine(L"{0} weighs {1} lbs.", name, weights[result]);
    		
    
    		// This outputs the corresponding weights for Bill, Eve, and Al respectively since
    		// they are a positive match from the BinarySearch function that searches through
    		// the list of names we want to search for and tries to find each individual name
    	
    	}
    
    	// The following is code that is meant to revise the previous code in order to
    	// be able to add Fred's name into the names string array
    
    	String^ name = L"Fred"; 
    
    	// This creates the name which we want to insert into the array
    
    	
    	int position = Array::BinarySearch(names, name); 
    
    	// This will create an index marker variable that will store the current value of 
    	// Fred's binary value
    	
    	if(position < 0) 
    		
    		position = ~position;
    
    	// This will flip Fred's binary value to become a positive result that we can use
    	// to insert into the newNames list
    
    
    	array<String^>^ newNames = gcnew array<String^>(names -> Length+1); 
    
    	// This creates a newNames string array, that will store names with an added
    	// length of 1 so we can add Fred's name into the array
    	
    	for(int i = 0; i < position; i++) 
    
    		// This will go through each of teh names in the position
    		
    		newNames[i] = names[i]; 
    
    	// This is basically copying each individual index value from the previous
    	// names array into the newNames array
    	
    	newNames[position] = name; 
    
    	// This will write the index value of the names list into the newNames list
    	
    	if(position < names -> Length) 
    
    		// This searches through the entire old names list
    		
    		for(int i = position; i < names -> Length; i++) 
    
    			// This goes throughout the entire name list again along the entire length
    			// of it
    			
    			newNames[i+1] = names[i];
    
    			// This will assign the next index value to the current names index position
    
    	Console::WriteLine("This is the value of the new names list: {0}", newNames);
    
    	// This is just my own output statement that will output the new names list,
    	// but it is giving me A1 apparently for some reason
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    83
    The issue I'm having is that when I attempt to output the array at the end of the program, I'm receiving a "A1".

    Is this some sort of representation of Fred's position in Hexadecimal (sounds really vague, and probably wrong, but I have no idea as to how this occurred)
    No, you're not outputting A1, you're outputting AL. The names array was previously sorted. So now the names array appears as follows.. AL, Bill, Dan, Eve, Fred etc.

    That's the letter L after the A not the numeric ONE. Since you've copied names to newNames, your Console.WriteLine will print out the first name in the array which happens to be AL with the letter L.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    83
    If you want to print out the complete list, you could use the following code...


    Code:
    	for(int x = 0; x < newNames->Length; x++)
    		 Console::WriteLine("{0}", newNames[x]);

IMN logo majestic logo threadwatch logo seochat tools logo