| | |
Struct in Functions
Thread Solved |
•
•
Join Date: Dec 2005
Posts: 1
Reputation:
Solved Threads: 0
Dear all;
i have a problem with functios. i have a program to write using struct in functions with arrays also. i have the program written in structes with array but not using the functions and i don't no who the do it with functions. if anybody acn help i will appreciate it very much. I've been having a big heduce from this problem becuse i don't know how to solve it and also because i only have 1 day to give it for the professor.
CAN ANYBODY HELP ME IN THIS PROBLEM
i have a problem with functios. i have a program to write using struct in functions with arrays also. i have the program written in structes with array but not using the functions and i don't no who the do it with functions. if anybody acn help i will appreciate it very much. I've been having a big heduce from this problem becuse i don't know how to solve it and also because i only have 1 day to give it for the professor.
C Syntax (Toggle Plain Text)
include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct Household { int Noperson; string letters; double income; }; int main() { ifstream in; ofstream out; int choice_input,counter, counter2,counter3,index,size; double total,average,plevel,percentage; Household Survey [50]; Household Survey2 [50]; int temp; string ctemp; in.open("f:\\example.txt"); out.open("f:\\Rfexample.txt"); if(!in) { cout<<"The file cannot be open."<<endl; return 1; } counter=0; while (!in.eof() && counter<50) { in>>Survey[counter].letters; in>>Survey[counter].income; in>>Survey[counter].Noperson; counter++; } for(size = 0; size < counter; size++) { Survey2[size].letters = Survey[size].letters; Survey2[size].income = Survey [size].income; Survey2[size].Noperson = Survey[size].Noperson; } total = 0; for(size=0; size < counter; size++) { total=Survey[size].income + total; } average= total/counter; for(size=0; size < counter-1; size++) { for( index = 0; index < counter - 1 - size;index++) { if(Survey2[index].income > Survey2[index+1].income) { temp = Survey2[index].income; Survey2[index].income = Survey2[index+1].income; Survey2[index+1].income = temp; ctemp = Survey2[index].letters; Survey2[index].letters = Survey2[index+1].letters; Survey2[index+1].letters = ctemp; temp = Survey2[index].Noperson; Survey2[index].Noperson = Survey2[index+1].Noperson; Survey2[index+1].Noperson = temp; } } } out<< fixed << showpoint << setprecision(2); do { cout<<"1. Print all of the input data \n"; cout<<"2. Calculate the average household and find whose income is greater than average \n"; cout<<"3. Find the percentage of household income \n"; cout<<"4. Print all of the input data sorted \n"; cout<<"5. Calculate and print the median household \n"; cout<<"6. Quit \n"; cout<<"Please select a choice: "; cin>>choice_input; if(choice_input==1) { for(counter2 = 0; counter2 < counter; counter2++) { out << Survey[counter2].letters<<"\t"; out << Survey[counter2].income<<"\t"; out << Survey[counter2].Noperson<<"\t"; out << endl; cout << Survey[counter2].letters<<"\t"; cout << Survey[counter2].income<<"\t"; cout << Survey[counter2].Noperson<<"\t"; cout << endl; } } if(choice_input==2) { out<<"\nThe total is: "<<total; out<<"\nThe average is: "<<average; out<<endl; cout<<"\nThe total is: "<<total; cout<<"\nThe average is: "<<average; cout<<endl; for(size=0; size < counter; size++) { if( Survey[size].income > average) { out <<"\nThe Above averge is:"<<endl; out << Survey[size].letters<<"\t"; out << Survey[size].income<<"\t"; out << Survey[size].Noperson<<"\t"; out << endl; cout <<"\nThe Above averge is:"<<endl; cout << Survey[size].letters<<"\t"; cout << Survey[size].income<<"\t"; cout << Survey[size].Noperson<<"\t"; cout << endl; } } } if(choice_input==3) { counter3=0; for(size=0; size < counter; size++) { plevel=8000.00 + 500.00 * (Survey[size].Noperson - 2); if(Survey[size].income < plevel) { counter3++; } } percentage= (static_cast<double>(counter3) / counter)*100; out<<"\nThe percentage is: "<<percentage<<endl; cout<<"\nThe percentage is: "<<percentage<<endl; } if(choice_input==4) { for(size = 0; size<counter; size++) { out<<Survey2[size].letters<<"\t"; out<<Survey2[size].income<<"\t"; out<<Survey2[size].Noperson<<"\t"; out << endl; cout<<Survey2[size].letters<<"\t"; cout<<Survey2[size].income<<"\t"; cout<<Survey2[size].Noperson<<"\t"; cout << endl; } } if(choice_input==5) { double median; median=(Survey2[8].income + Survey2[9].income)/2; out<<"\nThe median is: "<<median<<endl; cout<<"\nThe median is: "<<median<<endl; } if (choice_input <1 || choice_input>6) // to state that choice was wrong in the main menu. cout<<"\n\n You've entered a wrong choice! Please choose again. \n\n"; }while (choice_input !=6 ); in.close(); out.close(); return 0; }
•
•
Join Date: May 2004
Posts: 178
Reputation:
Solved Threads: 10
It's not clear to me what you want. But here is very basic code for structs.
C Syntax (Toggle Plain Text)
struct Household { int Noperson; char letters[20]; double income; }; /* find the maximum income */ struct Household *foo(struct Household *src, int cnt) { struct Household *p=src; struct Household *retval=src; double max = -999999. int i=0; while(i<=cnt) { if(p->income > max) { max=p->income; retval=p; } cnt ++; p++; } return retval; } int main() { struct Household arr[100]={0,NULL,0.} struct Household *p=arr; int cnt=0; int i=0; for (i=0;i<100; i++, p++) { p->Noperson=i; p->income= i*2.; strcpy(p-Letters,"Hi there"); cnt++; } p=foo(arr,cnt); printf("Maximum income %f\n",p->income); }
![]() |
Similar Threads
- struct functions - lost a wee bit (i hope) in calling the functions (C++)
- help with creating and calling a function (C++)
- Help needed on struct functions (C)
- check this code pleeeeeeeeeease (C++)
- Trying to use pointers (C++)
- Double Linked Lists and Functions required (C++)
Other Threads in the C Forum
- Previous Thread: data sorts in reverse order!! help!!
- Next Thread: Seg Fault ~ Linked List Delete
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o ide inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h





