Copying 2-d arrays

This is a discussion on Copying 2-d arrays within the C++ Programming forums, part of the General Programming Boards category; i've got an assignment that requires me to copy an array into another array, then sort the original array and ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    12

    Copying 2-d arrays

    i've got an assignment that requires me to copy an array into another array, then sort the original array and output both of them so that you can see the difference. I know how to copy 1-d arrays, but not 2-d. 1-d:

    Code:
     void copyArray(double marks[][2], double marks2[][2], int numMarks)
    {
    	int i=0;
    	for(i=0; i<(numMarks-1); i++)
    	{
    		marks[i][0] = marks2[i][0];
    		marks[i][1] = marks2[i][1];
    	}
    }
    this is my function, the arrays are marks[20][2] and marks2[20][2]. Marks2 is originally unpopulated. marks is populated with user input.

    thanks in advance.

  2. #2
    Registered User
    Join Date
    Mar 2008
    Posts
    12

    mistake

    exclude the 1-d: before the code.

    thanks

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Is there a question? Your code looks ok, although I think you might want to use i < numMarks as your control rather that i < (numMarks-1). Also, make sure you're assigning from and to the correct arrays.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Values on the right-hand side of = is assigned to the variable on the left side, not the other way round.

    In addition, why numMarks - 1?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    12

    the ouput

    The output is a bunch of jibberish, as if it isn't copying them in properly. When changing it to numMarks it still outputs the same thing, but i think you're right to have it that way.

    is there a specific template you would use to copy them in?

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You get gibberish because you are writing uninitialized values over user input, not the other way round. See my previous post.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    12

    still not working.....

    The output is no longer jiberish, just zero's. Any ideas?

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    May-be you could post the full code so far?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    12

    entire code

    Code:
    #include<stdio.h>
    #include<math.h>
    double marks [20][2];
    char grade[20];
    int histogram[6];
    double averageMark (double marks[20][2], int numMarks);
    double standardDev (double marks[20][2], int numMarks, double average);
    double findMax (double marks[20][2], int numMarks);
    double findMin (double marks[20][2], int numMarks);
    void diffFromAverage (double marks[20][2], double average, double standardDeviation, int numMarks);
    void printTable (double marks[20][2], int numMarks);
    void printTable2 (double marks2[20][2], int numMarks);
    #define LIMIT 100
    void findGrade (double marks[20][2], int numMarks);
    int bubbleSort (double marks[20][2], int numMarks); 
    int binarySearch (double marks [20][2], int numMarks, double item);
    void copyArray (double marks [20][2], double marks2[20][2], int numMarks);
    double marks2[20][2];
    void main()
    {
    	int i=0;
    	int count=0;
    	double marks[20][2];
    	int numMarks;
    	double average;
    	double standardDeviation;
    	double maxvalue;
    	double minvalue;
    	char grade [20];
    	char choice;
    	double item;
    	int location;
    	char choice1;
    	double marks2[20][2];
    	
    	
    	printf("\nPlease enter the mark (-ive to end entry): ");	
    	scanf("%lf", &marks[i][0]);
    	if (marks[i][0] > LIMIT)
    	{
    		printf("\nPlease enter only numbers between 0 and 100");
    		printf("\nPlease enter the mark (-ive to end entry): ");	
    			scanf("%lf", &marks[i][0]);
    	}
    
    	while (i < 20 && marks[i][0] >= 0)
    	{
    		i++;
    
    		printf("\nPlease enter the mark (-ive to end entry): ");
    			scanf("%lf", &marks[i][0]);
    		if (marks[i][0] > LIMIT)
    		{
    		printf("\nPlease enter only numbers between 0 and 100");
    		printf("\nPlease enter the mark (-ive to end entry): ");	
    			scanf("%lf", &marks[i][0]);
    		}
    
    	}
    
    	count = i;
    	i--;
    	numMarks = count;
    
    
    		printf("\nYou've entered %d marks", numMarks);
    
    		average = averageMark (marks, numMarks); 
    		standardDeviation = standardDev (marks, numMarks, average);
    		maxvalue = findMax(marks, numMarks);
    		minvalue = findMin(marks, numMarks);
    		
    		diffFromAverage (marks, average, standardDeviation, numMarks);
    
    		printf("\nThe average mark is %.2lf", average);
    		printf("\nThe standard deviation of the marks is %.2lf", standardDeviation);
    		printf("\nThe maximum mark is %.2lf", maxvalue);
    		printf("\nThe minimum mark is %.2lf", minvalue);
    
    		findGrade(marks, numMarks);
    
    		copyArray(marks, marks2, numMarks);
    		
    		bubbleSort(marks, numMarks);
    
    		findGrade(marks, numMarks);
    		
    		printf("\nThis is the original array, unsorted, copied to a second array named marks2: \n");
    		printTable2(marks2, numMarks);
    
    		printf("\nThis is the sorted array: \n");
    		printTable(marks, numMarks);
    
    		
    		printf("\nWould you like to search for a mark within the list?(Y/N): ");
    		fflush(stdin);
    		scanf("%c", &choice);
    
    		if (choice == 'y' || choice == 'Y')
    		{
    			printf("\nPlease enter the mark that you are searching for: ");
    			fflush(stdin);
    			scanf("%lf", &item);
    			
    			location = binarySearch (marks, numMarks, item);
    			
    			if (location > -1)
    				printf("\nThe mark was found at location %d", location);
    			else
    				printf("\nThe mark was not found in the list");
    		}
    
    		while (location == -1)
    		{
    			printf("\nWould you like to search for another mark?(Y/N): ");
    			fflush(stdin);
    			scanf("%c", &choice1);
    			if (choice1 == 'y' || choice1 == 'Y')
    			{
    				printf("\nPlease enter the mark that you are searching for: ");
    				fflush(stdin);
    				scanf("%lf", &item);
    				location = binarySearch (marks, numMarks, item);
    			if (location > -1)
    				printf("\nThe mark was found at location %d", location);
    			else
    				printf("\nThe mark was not found in the list");
    			}
    		}
    
    		
    		printf("\nEnd of program output.");
    		
    		getchar();
    
    	
    }
    
    double averageMark (double marks[][2],int num)
    {
    	double totalofmarks = 0.0;
    	double average;
    	int i=0;
    	while (i < (num-1) || i == (num-1))
    	{
    		totalofmarks += marks[i][0];
    		i++;
    	}
    	return(totalofmarks/num);
    
    }
    
    double standardDev(double marks[][2], int num, double average)
    {
    	double sumdevs = 0.0;
    	int i=0;
    	while (i < (num-1) || i == (num-1))
    	{
    		sumdevs = sumdevs + pow((marks[i][0] - average),2);
    		i++;
    	}
    	return(sqrt(sumdevs/num));
    }
    
    double findMax(double marks[][2], int num)
    {
    	int i=1;
    	double max;
    	max=marks[0][0];
    
    	while(i < num-1)
    	{
    		if (max < marks[i][0])
    			max = marks[i][0];
    	i++;
    	}
    	return(max);
    }
    
    double findMin(double marks[][2], int num)
    {
    	int i=1;
    	double min;
    	min=marks[0][0];
    
    	while (i < num)
    	{
    		if (min > marks[i][0])
    			min = marks[i][0];
    
    	i++;
    	}
    	return(min);
    }
    
    void diffFromAverage (double marks[][2], double average, double standardDeviation, int num)
    {
    	int i=0;
    	while(i < num)
    	{
    		marks[i][1] = ((marks[i][0] - average) / standardDeviation);
    		i++;
    	}
    }
    
    void printTable (double marks[][2], int numMarks)
    {
    	int i,j;
    	
    	printf("\nMark		Difference from Average		Letter Grade");
    	printf("\n		(in std deviations)");
    	printf("\n====		=======================		============");
    	
    	for (i=0; i < numMarks; i++)
    	{
    		printf("\n");
    		
    		printf("%.2lf				%.2lf			%c",marks[i][0], marks[i][1], grade[i]);
    	}
    }
    
    void printTable2 (double marks2[][2], int numMarks)
    {
    	int i,j;
    	
    	printf("\nMark		Difference from Average		Letter Grade");
    	printf("\n		(in std deviations)");
    	printf("\n====		=======================		============");
    	
    	for (i=0; i < numMarks; i++)
    	{
    		printf("\n");
    		
    		printf("%.2lf				%.2lf			%c",marks[i][0], marks[i][1], grade[i]);
    	}
    }
    
    
    void findGrade (double marks[][2], int numMarks)
    {
    	int i=0;
    	while (i<numMarks)
    	{
    		if (marks[i][0] > 85 || marks[i][0] == 85)
    			grade[i] = 'A';
    		else
    			if (marks[i][0] > 75 || marks[i][0] == 75)
    			grade[i] = 'B';
    		else
    			if (marks[i][0] > 65 || marks[i][0] == 65)
    			grade[i] = 'C';
    		else
    			if (marks[i][0] > 50 || marks[i][0] == 50)
    			grade[i] = 'D';
    		else
    			if (marks[i][0] < 50)
    			grade[i] = 'F';
    	i++;
    	}
    }
    
    int bubbleSort (double marks[][2], int numMarks)
    {
    	int i, j, moves = 0;
    	double temp;
    
    	for(i=0; i < (numMarks-1); i++)
    	{
    		for(j=1; j < numMarks; j++)
    		{
    			if(marks[j][0] > marks[j-1][0])
    			{
    				temp = marks[j][0];
    				marks[j][0] = marks[j-1][0];
    				marks[j-1][0] = temp;
    				moves++;
    			}
    		}
    	}
    return(moves);
    }
    
    
    #define FALSE 0
    #define TRUE 1
    int binarySearch (double marks[][2], int numMarks, double item)
    {
    	int index, found, left, right, midpoint;
    
    	index = -1;
    	found = FALSE;
    	left = 0;
    	right = (numMarks-1);
    
    	while (left <= right && !found)
    	{
    		midpoint = (int) ((left + right)/2);
    		if (item == marks[midpoint][0])
    		{
    			found = TRUE;
    			index = midpoint;
    		}
    		else if (item > marks[midpoint][0])
    			right = midpoint - 1;
    		else if (item < marks[midpoint][0])
    			left = midpoint + 1;
    	}
    	return(index);
    }
    
    void copyArray(double marks[][2], double marks2[][2], int numMarks)
    {
    	int i=0;
    	for(i=0; i<numMarks; i++)
    	{
    		marks2[i][0] = marks[i][0];
    		marks2[i][1] = marks[i][1];
    	}
    }

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    thanks for all the help ANON, i just figured it out. In printTable2 i was including the original array, and not marks2. Thanks for helping me trouble shoot this......i'm just a beginner C programmer and i find alot of this very confusing. In fact, i've been working on this code for two full days if you can believe it. i hope it gets better for me.....

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Don't use void main. Use int main.
    And don't flush stdin. That's undefined behavior.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The problem is the two print functions. One of them is printing the global (and unused) array.

    You seem to have some sort of deeper misunderstanding. Firstly, you are passing arrays and variables to each function (as you should). Therefore you don't need the globals at all. In this case, if the needless globals weren't there you would have got a compiler error pointing you to the problem line.

    Secondly, I don't see how printTable and printTable2 differ. You can pass different arrays to a single function (and the names inside the function and in the calling function don't have to be the same). So, you now probably see how copy'n'paste programming backfires.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is this poor coding? copying arrays of doubles
    By Hansie in forum C Programming
    Replies: 12
    Last Post: 05-25-2007, 03:34 PM
  2. Copying character arrays
    By neandrake in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2005, 06:32 PM
  3. Copying Arrays
    By conright in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 04:09 PM
  4. help on copying arrays
    By neversell in forum C Programming
    Replies: 3
    Last Post: 06-30-2002, 05:22 PM
  5. copying character arrays
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-20-2002, 06:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21