-
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.
-
mistake
exclude the 1-d: before the code.
thanks
-
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.
-
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?
-
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?
-
You get gibberish because you are writing uninitialized values over user input, not the other way round. See my previous post.
-
still not working.....
The output is no longer jiberish, just zero's. Any ideas?
-
May-be you could post the full code so far?
-
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];
}
}
-
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.....
-
Don't use void main. Use int main.
And don't flush stdin. That's undefined behavior.
-
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.