Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,053 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,572 people online right now. Registration is fast and FREE... Join Now!




Import file data into an array

2 Pages V  1 2 >  
Reply to this topicStart new topic

Import file data into an array

webmin
1 Aug, 2008 - 12:04 PM
Post #1

D.I.C Head
**

Joined: 21 May, 2008
Posts: 96


My Contributions
The requirements for this program was:

Write a program to answer inquires about student data. Using a menu driven interface, provide the capability to print out the scores, average, or grade for a student. A fourth menu option is to provide all information about a given student. All array functions are to receive the array as a pointer and use pointer arithmetic. The data will be stored in a 2d array...

Here's my checklist for what I've already done:
  • Menu Driven Interface: Done!
  • Opened file stddata.txt and stored data in variables: snum, qz1, qz2, qz3, qz4, qz5: Done!
  • Stored variables from previous step in a 2d array stddata[15][6]: NOT DONE!
This is where I'm stuck. I'm not sure how to properly initialize a 2d array in a function (At both spots function declaration & function initialization), and then set column 1=snum, column 2=qz1, column 3=qz2, column 4=qz3, column 5=qz4, column 6=qz5.

Just to show a little effort on my part, here's what I've tried writting:
cpp

//Function Declaration
void setequal (int* stable, int snum, int qz1, int qz2, int qz3, int qz4, int qz5);

void setequal (int* stable, int snum, int qz1, int qz2 ,int qz3, int qz4, int qz5)
{
int r = 15, c = 6;
int row, column;
int stddata[r][c];

*stable = stddata[r][c];
snum = stddata[0][0];
qz1 = stddata[0][1];
qz2 = stddata[0][2];
qz3 = stddata[0][3];
qz4 = stddata[0][4];
qz5 = stddata[0][5];

}


*stable was intended to be a pointer to that array so that I could just use it throughout the rest of my program but I'm not sure if I did that right. I also wanted to make sure that this code would do what I think it would and place the values that I scanned in from file into the array.

The next step will be to create a search algorithm, and by chance is there a way to do this and make it into a header file. Searching seems to be a major part of C programs and it would be nice to be able to include that as a header file to simplify programming techniques.

Anyways thanks for any help you can give, and please make sure ideas posted are done in C language. Thanks

This post has been edited by webmin: 1 Aug, 2008 - 12:06 PM
User is offlineProfile CardPM
+Quote Post

webmin
RE: Import File Data Into An Array
1 Aug, 2008 - 04:21 PM
Post #2

D.I.C Head
**

Joined: 21 May, 2008
Posts: 96


My Contributions
Ok I've sort of answered one of my own questions. I know that to get the info I need into my array I need to use the sprintf command under stdio.h I also found the search.h header file and how to use that properly.

Now I'm down to two questions that I can think of. The first one is can someone show me the proper way to use this sprintf with my code or something simliar to it so that I can get an idea of what I need to implement in my own program.

Question two: when I search for the student number, how can I get it to also give the row that this student corresponds to.
User is offlineProfile CardPM
+Quote Post

webmin
RE: Import File Data Into An Array
2 Aug, 2008 - 05:48 AM
Post #3

D.I.C Head
**

Joined: 21 May, 2008
Posts: 96


My Contributions
I've decided against printing the values into my array. So now all I need to know is how can I search for a student, and have it return the row to me as well.

I'd also like to be able to take the last 5 values in that row and either print them, get an average & print that average, or get an average, convert it to a letter grade & print the letter grade.

Any suggestions would be helpful as long as they're in C. Thanks
User is offlineProfile CardPM
+Quote Post

webmin
RE: Import File Data Into An Array
2 Aug, 2008 - 07:31 AM
Post #4

D.I.C Head
**

Joined: 21 May, 2008
Posts: 96


My Contributions
Wait I realized that my project has changed too much so let me start over and show what I've done so far what I still need to do.

Here's my program as it was written so far:
cpp

#include <stdio.h>
#include <search.h>
#include <stdlib.h>
#include <stddef.h>
#include <conio.h> //for getchar();
#define FLUSH while(getchar() != '\n')

void menu();
int search (void);
int compare (int *x, int *y)
{
return (* x -*y);
}
int printscr (void);
int printavg (void);
int printgrd (void);
int printinfo (void);
int extprog (void);

int main (void)
{
int select, selectIn;

do
{
menu();
printf("Please choose option 1-5:\n");
scanf_s("%d", &select);

switch(select)
{
case 1: selectIn = printscr();
break;
case 2: selectIn = printavg();
break;
case 3: selectIn = printgrd();
break;
case 4: selectIn = printinfo();
break;
case 5: selectIn = extprog();/*system("cls");
printf("Have a nice day.\n");
selectIn = 0;*/
break;
default: FLUSH;
system("cls");
printf("\aNot recognized. Please try again.\n");
selectIn = 1;
}
}
while(selectIn);
return 0;

}
void menu (void)
{
printf("\t\t\tWelcome to Grade Book V1.0.\n\t\tPlease tell me what we're going to do today?\n\n");
printf("\t\t*********************************************\n");
printf("\t\t* 1:Print A Students Scores *\n");
printf("\t\t* 2:Print A Students Average *\n");
printf("\t\t* 3:Print A Students Letter Grade *\n");
printf("\t\t* 4:Look up a Students info *\n");
printf("\t\t* 5:Quit *\n");
printf("\t\t*********************************************\n");

return;
}
int search (void)
{
int sdata[15][6] =
{
{1234, 52, 7, 100, 78, 34},
{1947, 45, 40, 88, 78, 55},
{2134, 90, 36, 90, 77, 30},
{2877, 55, 50, 99, 78, 80},
{3124, 100, 45, 20, 90, 70},
{3189, 22, 70, 100, 78, 77},
{4532, 11, 17, 81, 32, 77},
{4602, 89, 50, 91, 78, 60},
{5405, 11, 11, 0, 78, 10},
{5678, 20, 12, 45, 78, 34},
{6134, 34, 80, 55, 78, 45},
{6999, 0, 98, 89, 78, 20},
{7874, 60, 100, 56, 78, 78},
{8026, 70, 10, 66, 78, 56},
{9893, 34, 9, 77, 78, 20}
};

size_t elems = 90;
int studentnumber;
int *find;

system("cls");
printf("Please enter a Student Number for more info: ");
scanf_s("%d", &studentnumber);

find = (int *)_lfind (& studentnumber, &sdata, &elems,
sizeof (int),
(int(*) (const void *,
const void *)) compare);
if (find)
printf ("Student %d found in linear search.\n",
studentnumber);
else
{
printf ("Could not find student %d. Press enter to try again.\n", studentnumber);
system("pause");
search();
}
return 1;
}
int printscr (void)
{
search();
printf("\n\n");
return 1;
}
int printavg (void)
{
return 1;
}
int printgrd (void)
{
return 1;
}
int printinfo (void)
{
return 1;
}
int extprog (void)
{
int ans;

system("cls");

printf("Are you sure you want to quit? 0 to quit, 1 to return to main menu.\n");
scanf_s("%d", &ans);

if (ans == 0)
{
return 0;
}
else if (ans == 1)
{
return 1;
}
else
{
system("cls");
printf("Sorry Invalid choice\n");
return 1;
}
}


Now in the search function I would like to create a pointer such as *snumber and set that equal to sdata[0-14][0]. Meaning that I would like to be able to enter a student number such as 1234, and it would return values 52, 7, 100, 78, 34 with it as well.

I would also like to be able to take those returned values and calculate things like an average, a grade, or just print the scores.

Basically I need to be able to extract a row from a 2d array.

if someone can point me in the right direction I'd be very happy to finish this project. Thanks.

This post has been edited by webmin: 2 Aug, 2008 - 11:24 AM
User is offlineProfile CardPM
+Quote Post

KYA
RE: Import File Data Into An Array
2 Aug, 2008 - 06:04 PM
Post #5

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,776



Thanked: 91 times
Dream Kudos: 1200
My Contributions
This is how I would handle that portion of the project:

The problem is that there is no association between finding a student #/ID and its location within your array.

Here is your code with my modifications:

cpp

#include <stdio.h>
#include <search.h>
#include <stdlib.h>
#include <stddef.h>
#include <conio.h> //for getchar();
#define FLUSH while(getchar() != '\n')

void menu();
int search (void);
int compare (int *x, int *y)
{
return (* x -*y);
}
int printscr (void);
int printavg (void);
int printgrd (void);
int printinfo (void);
int extprog (void);
void displayStudentInfo(int studentnumber, int sdata[][6]);

int main (void)
{
int select, selectIn;

do
{
menu();
printf("Please choose option 1-5:\n");
scanf_s("%d", &select);

switch(select)
{
case 1: selectIn = printscr();
break;
case 2: selectIn = printavg();
break;
case 3: selectIn = printgrd();
break;
case 4: selectIn = printinfo();
break;
case 5: selectIn = extprog();/*system("cls");
printf("Have a nice day.\n");
selectIn = 0;*/
break;
default: FLUSH;
system("cls");
printf("\aNot recognized. Please try again.\n");
selectIn = 1;
}
}
while(selectIn);
return 0;

}
void menu (void)
{
printf("\t\t\tWelcome to Grade Book V1.0.\n\t\tPlease tell me what we're going to do today?\n\n");
printf("\t\t*********************************************\n");
printf("\t\t* 1:Print A Students Scores *\n");
printf("\t\t* 2:Print A Students Average *\n");
printf("\t\t* 3:Print A Students Letter Grade *\n");
printf("\t\t* 4:Look up a Students info *\n");
printf("\t\t* 5:Quit *\n");
printf("\t\t*********************************************\n");

return;
}
int search (void)
{
int sdata[15][6] =
{
{1234, 52, 7, 100, 78, 34},
{1947, 45, 40, 88, 78, 55},
{2134, 90, 36, 90, 77, 30},
{2877, 55, 50, 99, 78, 80},
{3124, 100, 45, 20, 90, 70},
{3189, 22, 70, 100, 78, 77},
{4532, 11, 17, 81, 32, 77},
{4602, 89, 50, 91, 78, 60},
{5405, 11, 11, 0, 78, 10},
{5678, 20, 12, 45, 78, 34},
{6134, 34, 80, 55, 78, 45},
{6999, 0, 98, 89, 78, 20},
{7874, 60, 100, 56, 78, 78},
{8026, 70, 10, 66, 78, 56},
{9893, 34, 9, 77, 78, 20}
};

size_t elems = 90;
int studentnumber;
int *find;

system("cls");
printf("Please enter a Student Number for more info: ");
scanf_s("%d", &studentnumber);

find = (int *)_lfind (& studentnumber, &sdata, &elems,
sizeof (int),
(int(*) (const void *,
const void *)) compare);
if (find)
{
printf ("Student %d found in linear search.\n",
studentnumber);
displayStudentInfo(studentnumber, sdata);
}
else
{
printf ("Could not find student %d. Press enter to try again.\n", studentnumber);
system("pause");
search();
}
return 1;
}
int printscr (void)
{
search();
printf("\n\n");
return 1;
}
int printavg (void)
{
return 1;
}
int printgrd (void)
{
return 1;
}
int printinfo (void)
{
search();
return 1;
}
int extprog (void)
{
int ans;

system("cls");

printf("Are you sure you want to quit? 0 to quit, 1 to return to main menu.\n");
scanf_s("%d", &ans);

if (ans == 0)
{
return 0;
}
else if (ans == 1)
{
return 1;
}
else
{
system("cls");
printf("Sorry Invalid choice\n");
return 1;
}
}

void displayStudentInfo(int studentnumber, int sdata[][6])
{
int studentID = studentnumber;
/*
* The number is found, iterate through array
* to display associated values
*/
for (int i = 0; i < 15; i++)
{
if (sdata[i][0] == studentID)
{
printf("Associated data with student: %d", studentnumber);
printf("\n");
for (int j = 1; j < 6; j++)
{
printf("%d", sdata[i][j]);
printf("\n");
}
}
}
}


Modified parts include:

cpp

void displayStudentInfo(int studentnumber, int sdata[][6])
{
int studentID = studentnumber;
/*
* The number is found, iterate through array
* to display associated values
*/
for (int i = 0; i < 15; i++)
{
if (sdata[i][0] == studentID)
{
printf("Associated data with student: %d", studentnumber);
printf("\n");
for (int j = 1; j < 6; j++)
{
printf("%d", sdata[i][j]);
printf("\n");
}
}
}
}

It compares the search value to the hard coded array you already had and displays associated information. You can format it anyway you want--I'll leave it up to you smile.gif

Unless its a project assignment, there is no need for a pointer to the array, you can do averages, etc... once you have located the correct 2nd dimension of the array through search.

If I read correctly, this is what you wanted. Enjoy!
User is online!Profile CardPM
+Quote Post

webmin
RE: Import File Data Into An Array
2 Aug, 2008 - 06:14 PM
Post #6

D.I.C Head
**

Joined: 21 May, 2008
Posts: 96


My Contributions
yes this is awesome.

My only concern is that my book states that all array functions are to receive the array as a pointer and use pointer arithmetic.

Does your solution do this or because I'm not using any functions that calls an array this step isn't neccessary?

I guess I could always declare *stable in the function declaration, and then in the actual function itself declare *stable = sdata[][6]

One more question, your void function that you built, where should that be called into the program, I didn't see it called by search, or by main, or any other function for that matter. Is it portable enough that I could do something like

cpp

int printscr (void)
{
search();
displayStudentInfo(int studentnumber, int sdata[][6]);
...
}


Again thanks for your help

This post has been edited by webmin: 2 Aug, 2008 - 06:21 PM
User is offlineProfile CardPM
+Quote Post

KYA
RE: Import File Data Into An Array
2 Aug, 2008 - 06:15 PM
Post #7

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,776



Thanked: 91 times
Dream Kudos: 1200
My Contributions
Let me make a quick change and illustrate some pointer arith.

On second thought, the function doesn't "call" the array per se.

This post has been edited by KYA: 2 Aug, 2008 - 06:16 PM
User is online!Profile CardPM
+Quote Post

webmin
RE: Import File Data Into An Array
2 Aug, 2008 - 06:23 PM
Post #8

D.I.C Head
**

Joined: 21 May, 2008
Posts: 96


My Contributions
sorry here's my edit while you made your new post:
QUOTE

I guess I could always declare *stable in the function declaration, and then in the actual function itself declare *stable = sdata[][6]

One more question, your void function that you built, where should that be called into the program, I didn't see it called by search, or by main, or any other function for that matter. Is it portable enough that I could do something like

cpp
view plainprint?

1. int printscr (void)
2. {
3. search();
4. displayStudentInfo(int studentnumber, int sdata[][6]);
5. ...
6. }


int printscr (void)
{
search();
displayStudentInfo(int studentnumber, int sdata[][6]);
...
}



Again thanks for your help

User is offlineProfile CardPM
+Quote Post

KYA
RE: Import File Data Into An Array
2 Aug, 2008 - 06:25 PM
Post #9

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,776



Thanked: 91 times
Dream Kudos: 1200
My Contributions
Search calls the display function if the number is found:

cpp

if (find)
{
printf ("Student %d found in linear search.\n",
studentnumber);
displayStudentInfo(studentnumber, sdata[][6]);
}


So a successful search will display the associated values if you so chose.Simply take that function call out of search qand pop it whereever you choose smile.gif

edited for a typo

This post has been edited by KYA: 2 Aug, 2008 - 06:26 PM
User is online!Profile CardPM
+Quote Post

webmin
RE: Import File Data Into An Array
2 Aug, 2008 - 06:33 PM
Post #10

D.I.C Head
**

Joined: 21 May, 2008
Posts: 96


My Contributions
I see that's what I did. So now I need to look over this to get it to calculate an average, or a grade. If you have any suggestions, I'd appreciate them.

Thanks
User is offlineProfile CardPM
+Quote Post

KYA
RE: Import File Data Into An Array
2 Aug, 2008 - 06:50 PM
Post #11

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,776



Thanked: 91 times
Dream Kudos: 1200
My Contributions
Declare a few temp variables for avg, etc.. inside display (or outside and pass them in)
Inside the second for loop calculate the second deminsion values as ypou iterate through them.

Boom--done!
User is online!Profile CardPM
+Quote Post

webmin
RE: Import File Data Into An Array
2 Aug, 2008 - 07:08 PM
Post #12

D.I.C Head
**

Joined: 21 May, 2008
Posts: 96


My Contributions
QUOTE(KYA @ 2 Aug, 2008 - 10:50 PM) *

Declare a few temp variables for avg, etc.. inside display (or outside and pass them in)
Inside the second for loop calculate the second deminsion values as ypou iterate through them.

Boom--done!


Not quite sure I follow

I should declare
cpp

float avg;
int sum;

for (int i = 0; i < 15; i++)
sum += sdata[][6]; // not sure if this is right
avg = (float)sum / 5;


Does that look like what you were thinking
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/1/08 05:45PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month