Jump to content

Need help with a class assignment

- - - - -

  • Please log in to reply
2 replies to this topic

#1
EonsNearby

EonsNearby

    Newbie

  • Members
  • Pip
  • 5 posts
I was given a class assignment over my spring break (which is this week), and I am having trouble doing the programming warm up exercises, and I was wondering if someone could help me out. I used Visual Studio 2010 to make this program and the programming language is C++ (the class I am taking for it is Software Design and Development [which is basically an intro to C++ class]).

1) After it completes the test for "PWU Exercise 7", it seems to crash for some reason, but I cannot decipher why

2) 2) For PWU Exercise 9, I am supposed to write a function that will ask the user to input a value that will be used to create the length of an array. However, since I can only use an immediate or constant expression as the length initializer, I am not sure how I can do so. I tried defining a constant that is initialized to the variable that contains the input from the user, but that did not work.


// InClass Assignment 5

// 3/11/2011

// 


#include <iostream>


#include <cstring>


#include <string>


#include <stdio.h>


#include <stdlib.h>


#include "Phone.h"


using namespace std;


bool ShallowCompare (Phone*, Phone*);


bool DeepCompare (Phone*, Phone*);


int Greatest (int[], int);


int GetGreatestInput();


int main()

{

	int static max;


	// PWU Exercise 1

	int someInt;

	

	int* intPointer;


	intPointer = &someInt;


	someInt = 451;


	if(someInt == 451)

		cout << "Passed PWU Exercise 1." << endl;

	else

		cout << "Failed PWU Exercise 1." << endl;


	someInt = 0;


	*intPointer = 451;


	if(someInt == 451)

		cout << "Passed PWU Exercise 1.\n" << endl;

	else

		cout << "Failed PWU Exercise 1.\n" << endl;


	// PWU Exercise 2

	char* charArrPointer;


	char initials[4];


	charArrPointer = initials;


	*charArrPointer = 'A';


	*(charArrPointer + 1) = 'E';


	*(charArrPointer + 2) = 'W';


	if (*charArrPointer == 'A')

		cout << "Passed PWU Exercise 2.\n" << endl;

	else

		cout << "Failed PWU Exercise 2.\n" << endl;


	// PWU Exercise 3

	Phone newPhone;


	Phone* structPointer = &newPhone;


	structPointer->country = 1;


	structPointer->area = 888;


	structPointer->number = 5551212;


	if ((structPointer->country == 1) && (structPointer->area == 888) && (structPointer-> number == 5551212))

	{

		cout << "Passed PWU Exercise 3.\n" << endl;

	}

	else

	{

		cout << "Failed PWU Exercise 3.\n" << endl;

	}


	// PWU Exercise 4

	Phone& structReference = newPhone;


	structReference.country = 1;


	structReference.area = 888;


	structReference.number = 5551212;


	if ((structReference.country == 1) && (structReference.area == 888) && (structReference.number == 5551212))

	{

		cout << "Passed PWU Exercise 4.\n" << endl;

	}

	else

	{

		cout << "Failed PWU Exercise 4.\n" << endl;

	}


	// PWU Exercise 5

	Phone* structPointer1 = &newPhone;


	Phone* structPointer2 = &newPhone;

	

	if (ShallowCompare(structPointer1,structPointer2))

	{

		cout << "Passed PWU Exercise 5.\n" << endl;

	}

	else

	{

		cout << "Failed PWU Exercise 5.\n" << endl;

	}


	// PWU Exercise 6

	Phone* structPointer3 = &newPhone;


	Phone* structPointer4 = &newPhone;


	if (DeepCompare(structPointer3, structPointer4))

	{

		cout << "Passed PWU Exercise 6.\n" << endl;

	}

	else

	{

		cout << "Failed PWU Exercise 6.\n" << endl;

	}


	// PWU Exercise 7

	int exercise7Array[100];


	int* dataPtr = exercise7Array;


	for (int count = 0; count < 100; count++)

	{

		*(dataPtr + count) = count;

	}


	for (int count = 0; count < 100; count++)

	{

		int temporary;


		temporary = *(dataPtr + count);


		if (count == 0)

		{

			temporary = max;

		}


		else

		{

			if (max < temporary)

			{

				max = temporary;

			}

		}

	}


	if (max == 99)

		cout << "Passed PWU Exercise 7.\n" << endl;

	else

		cout << "Failed PWU Exercise 7.\n" << endl;


	delete dataPtr;


	// PWU Exercise 8

	int exercise8Array[100];


	int* dataPtr2 = exercise8Array;


	for (int count = 0; count < 100; count++)

	{

		*(dataPtr2 + count) = count;

	}


	max = Greatest(exercise8Array, 100);


	if (max == 99)

		cout << "Passed PWU Exercise 8.\n" << endl;

	else

		cout << "Failed PWU Exercise 8.\n" << endl;


	// PWU Exercise 9

	int largestValue9 = GetGreatestInput();


	if (largestValue9 == 99)

		cout << "Passed PWU Exercise 9.\n" << endl;

	else

		cout << "Failed PWU Exercise 9.\n" << endl;


	// PWU Exercise 10



	// PWU Exercise 11

	int* oldValue;


	int* newValue;


	int exercise10Int1 = 40;


	int exercise10Int2 = 50;


	oldValue = &exercise10Int1;


	if (*oldValue != NULL)

	{

		newValue = oldValue;

	}


	else

	{

		newValue = &exercise10Int2;

	}


	if (*newValue == 50)

	{

		cout << "Passed PWU Exercise 11.\n" << endl;

	}


	else

	{

		cout << "Failed PWU Exercise 11.\n" << endl;

	}


	// PWU Exercise 12

	if (*oldValue == *newValue)

	{

		delete oldValue;

	}


	if (*oldValue != NULL)

	{

		cout << "Passed PWU Exercise 11.\n" << endl;

	}


	else

	{

		cout << "Failed PWU Exercise 11.\n" << endl;

	}


	// PWU Exercise 13



	// PWU Exercise 14



	// PWU Exercise 15



	// PWU Exercise 16



	// PWU Exercise 17



	// PWU Exercise 18



	// PWU Exercise 19



	// PWU Exercise 20



	system("Pause");


	return 0;

}


bool ShallowCompare (Phone* ptr1, Phone* ptr2)

{

	if(ptr1 == ptr2)

		return true;

	else

		return false;

}


bool DeepCompare (Phone* ptr1, Phone* ptr2)

{

	if ((ptr1->country == ptr2->country) && (ptr1->area == ptr2->area) && (ptr1->number == ptr2->number))

		return true;

	else

		return false;

}


int Greatest (int array1[], int size)

{

	int maximum;


	int* temporaryPtr = array1;


	for (int count = 0; count < size; count++)

	{

		int temporary;


		temporary = *(temporaryPtr + count);


		if (count == 0)

		{

			maximum = temporary;

		}


		else

		{

			if (maximum < temporary)

			{

				maximum = temporary;

			}

		}

	}


	delete temporaryPtr;


	return maximum;

}


int GetGreatestInput()

{

	int size;


	cout << "Input the number of values to be input: " << endl;


	cin >> size;


	cin.ignore(100, '\n');


	int temporaryArray[1000];


	for (int count = 0; count < size; count++)

	{

		int temporaryValue;


		cout << "Input an integer value to be put into the array: " << endl;


		cin >> temporaryValue;


		cin.ignore(100, '\n');


		temporaryArray[count] = temporaryValue;

	}


	int largestValue = Greatest(temporaryArray, size);


	return largestValue;

}


Edited by dargueta, 19 March 2011 - 01:08 AM.
Added code tags


#2
lintwurm

lintwurm

    Learning Programmer

  • Members
  • PipPipPip
  • 77 posts
Please use the code tags and also, where exactly is the problem?

#3
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Just took a quick look at your code (and like lintwurm said, please use code tags ( # button ) next time, makes it easier to read), and your DeepCompare function can be a simple one line:

bool DeepCompare(Phone* ptr1, Phone* ptr2) {

    return (ptr1->country == ptr2->country && ptr1->area == ptr2->area && ptr1->number == ptr2->number);

}


Also, your Greatest function is somewhat complicated, meaning it can be written nicer. :)

int Greatest(int array[], int size) {

    let max be first value of array

    loop through array, starting with second element

        if current element is greater than max

            swap current and max elements

    return max

}

Anyways, that how I would've done it but that's the beauty of programming - there's more than one way to solve problems. One important thing; you used delete on that pointer, remember this: when you use new to allocate memory, use delete on that object when you're done, and when new[] to create an array, use delete[].
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users