I have installed FC5 because the board of education in Israel has decided students who learn C and C++ should compile and run in Linux.
I like the GUI , simple enough to use .
however i downloaded an openoffice version who has hebrew support but i cant install it ..... it takes 122MB but no run-file , looks like and archive but i have no idea - new at this Linux business .
YoTaMiX 0 Light Poster
Noone's going to browse through your stuff and fix it all for you.
Copy relevant parts in code tags and show where you have problems, and people will most likely try to help.
public static int EliminatePlayers(int max,int min,int numOfPlayers)
{
int killCount=0;
int retValue=0;
for (int i=0;i<10;i++)
{
if (rollArray[i]==min)
{
players[i]=null;
rollArray[i]=0;
killCount++;
}
if (rollArray[i]==max)
{
players[i]=null;
rollArray[i]=0;
killCount++;
}
}
PrintEliminated();
System.out.printf("\n ---Next Round--- \n");
retValue=(numOfPlayers-killCount);
return retValue;
}
public static String ScanForWinner(int playersLeft)
{
int winnerIndex=10;
if (playersLeft==1) //Only 1 Player Remains as Winner
{
for (int i=0;i<10;i++)
{
if (rollArray[i]!=0) winnerIndex=i;
}
}
// End of 1 Player Case //
if (playersLeft==2) //2 Players Remain , Including Tie Break Operation
{
int tempMaxVal=rollArray[0];
int maxIndex=0;
for (int i=1;i<10;i++)
{
if (tempMaxVal<rollArray[i])
{
tempMaxVal=rollArray[i];
maxIndex=i;
}
rollArray[maxIndex]=0;
for (int j=0;j<10;j++)
{
if (rollArray[j]==tempMaxVal)
{
while (rollArray[j]==tempMaxVal)
{
rollArray[j]=RollDie();
rollArray[maxIndex]=RollDie();
if (tempMaxVal<rollArray[j])
{ winnerIndex=j; break; }
if (tempMaxVal>rollArray[j])
{ winnerIndex=maxIndex; break;}
}
if (tempMaxVal<rollArray[j])
{ winnerIndex=j; break; }
if (tempMaxVal>rollArray[j])
{ winnerIndex=maxIndex; break;}
}
}
}
winnerIndex=maxIndex;
}
return players[winnerIndex];
}
public void GamePlay()
{
int maxInRoll; //Max Value in Round
int minInRoll; //Min Value in Round
int maxCount=0; //Counts holding Max Value
int minCount=0; //Counts holding Min Value
int playersRemainCount=10; //Players Remains InGame Count
CreateRound(playersRemainCount); //First Round
while ((playersRemainCount>2)) //Condition for GamePlay to Continue
{
minInRoll=findMinInArray(); //Finds Min in Round
maxInRoll=findMaxInArray(); //Finds Max in Round
maxCount=CountMax(maxInRoll); //Counts for Max
minCount=CountMin(minInRoll); //Counts for Min
System.out.printf("MinCount is %d = Min %d , Max Count is …
YoTaMiX 0 Light Poster
Hello to you all ,
I am attaching a HW project which i made in Eclipse 3.2 Enviorment.
It is a Cube tourney that each game consists 10 players.
Round 1 is 100 Games , each winner is sent to the SemiFinal.
than 10 Games of Semi - each winner sent to final, than final game.
The rules : until 1 or 2 players remain , the players with Max Result and Min Result in each Randomized Roll of the Polygon ( 13 faces) are out.
conditions : if MinCount + MaxCount = PlayersRemainedCount there has to be a new Roll of the Polygon to all of them and re-check conditions.
If 1 players Remains - he wins. If 2 remain , the player with the highest roll result wins.
Bugs : Duplicate names in SemiFinal and Final games , somehow the are being repeated after i reset the array of rolls and names. I have spent 5 hours to try and find it ... i didnt. i am in a pool of poo :cheesy:
Thanx , i have 30 hours to submit it for a grade .(Wrapped in ZIP File)
Yotam , Israel .
This attachment is potentially unsafe to open. It may be an executable that is capable of making changes to your file system, or it may require specific software to open. Use caution and only open this attachment if you are comfortable working with zip files.
YoTaMiX 0 Light Poster
int start = 1; int max = 10; for ( int i = start; i < max + 1; i++ ) { for ( int j = start; j < max + 1; j++) { system.out.print ( i * j + "\t%d"); } system.out.println(""); }
My output:
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
Actually that don't work oops.
I wish to get 10 numbers from User and store them somehow without an array for that matter and get the same MultiTable to those numbers.
Its very tricky , i tried using some sort of SUM Varieble but couldnt make it work
YoTaMiX 0 Light Poster
* */ import java.util.Scanner; /** * @author Yotam Golomb ID 040435497 * */ public class MultiTableProg { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); int temp; int sum=1; System.out.println("Welcome to HomeWork 1."); System.out.println("Please Enter 10 Numbers , Range 1 to 50"); for (int i=1;i<11;i++) { temp=input.nextInt(); for (int j=1;j<11;j++) { System.out.printf("\t%d",sum*temp); sum=temp; } System.out.printf("\n"); } } }
What do u expect the code to do?
Well , i.e. :
if the input is numbers from 1 to 10 :
1 2 3 4 5 6 7 8 9 10
2 4 ...................20
3 6....................30
4 8....................40
5 10 .
6 12 .
7 14 .
8 16 .
9 18 .
10 20............... 100
I hope the Example is helpful.
thanx again , Yotam
YoTaMiX 0 Light Poster
Hello to all the Readers ,
I am trying to create a Multi-Table from 10 Numbers given by a user.
I cant figure it out why the Multitable isnt shown correctly. I tried
to enter the 1-10 numbers to get the result we all know and still nothing good came out.
thank you for you help , Code is Attached . :)
Yotam , Israel.
/**
*
*/
import java.util.Scanner;
/**
* @author Yotam Golomb ID 040435497
*
*/
public class MultiTableProg {
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
int temp;
int sum=1;
System.out.println("Welcome to HomeWork 1.");
System.out.println("Please Enter 10 Numbers , Range 1 to 50");
for (int i=1;i<11;i++)
{
temp=input.nextInt();
for (int j=1;j<11;j++)
{
System.out.printf("\t%d",sum*temp);
sum=temp;
}
System.out.printf("\n");
}
}
}
YoTaMiX 0 Light Poster
Nope, no code was attached. Why don't you just post it using code tags anyways?
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Queue
{
int first;
int full_size;
int present_size;
void** place;
}queue, *q_ptr;
//__ PROTOTYPES __//
char* MakeString();
q_ptr Create(int);
void Enqueue(void* Element,q_ptr Q);
void Dequeue(q_ptr Q);
void GetSize(q_ptr Q);
void ShowQ_Str(q_ptr Q);
void main()
{
q_ptr Q;
int choise,limit;
clrscr();
printf("Welcome to HW8 - String Example for Void* Queue\n");
printf("Please Choose of the Following : \n");
printf("1. Work with String \n2. Exit from HW8\n");
scanf("%d",&choise);
switch (choise)
{
case 1: {
int op=0;
printf("Enter Size you Wish To Limit Queue\n");
scanf("%d",&limit);
Q=Create(limit);
while (op!=4) //Repeat Menu
{
printf("Choose of the Following Operations : \n");
printf("1.Enqueue\n2.Dequeue\n3.Get Size of Queue\n4.Exit\n");
scanf("%d",&op);
switch (op)
{
case 1: { //Enqueue
char* Str=NULL;
Str=MakeString();
Enqueue(&Str,Q);
flushall();
break;
}
case 2: { Dequeue(Q); break; } //Dequeue
case 3: { GetSize(Q); break; } //Get Size
case 4: { ShowQ_Str(Q); getch(); free(Q->place); free(Q); exit(1); break; } //Exit Program
}
} //End Menu Of Queue
case 2: { printf("Bye Bye\n"); exit(1); break; } //Exit Program
}
}
}
void ShowQ_Str(q_ptr Q)
{
int j;
for (j=0;j<Q->present_size;j++)
printf("Q Place [%d] is %s \n",j,*(char*)(Q->place[j]));
}
char* MakeString()
{
char* temp;
printf("Enter String\n");
gets(temp);
return temp;
}
q_ptr Create(int limit_size)
{
q_ptr new_q;
new_q=(q_ptr)malloc(sizeof(queue));
if (new_q==NULL) { printf("Error in Queue Alloc , Bye \n"); exit(1); }
new_q->first=new_q->present_size=0;
new_q->full_size=limit_size;
return new_q;
}
void Enqueue(void* Element,q_ptr Q)
{
int i;
if ( (Q->first==0) )
{
Q->place=(void**)malloc(sizeof(void*));
if (Q->place==NULL)
{ printf("No Q 4 …
YoTaMiX 0 Light Poster
Hello to you all ,
I am Trying to Implement Code for Void** Array which will Store
Strings in a QUEUE system .
I can see the Strings in the Queue .
I am sure i dis the Casting OK, But i need a fresh pair of eyes.
Also is there a way to REALLOC The array each Dequeue?
Thanx
Yotam , Israel
P.S Code Attached - Using Regular C Compiler (Borland)
YoTaMiX 0 Light Poster
yes -- how else can the program pass the correct data type to printf(). There is another solution -- create a union of data types than an array of union objects
typedef union { short sVal; int iVal; long lVal; float fVal; double dVal; }DATA; ... DATA* array = malloc(some_number * sizeof(DATA)); ... scanf("%d",&array[loop].iVal);
so you are suggesting a Struct of Type to Use and DATA is the field itself?
1= sVal and so on? :-)
we are forced to use VOID* ARRAY ... :-)
anyhow , after i store the data , i need to find the two smallest values.
the prototype is :
void two_min(int(*comp)(void* a, void* b), void **answer, void **array);
and i need to compare functions for each type , 1 Int , 1 Float :
int Compare_Number( void* a , void* b)
{
return *(int*)a - *(int*)b;
}
and the same for float , right?
the requirement is that i need to go over the Data Array only once for each cell in array. is it possible?
YoTaMiX 0 Light Poster
thank you :-)
follow up question - if i wish to print out the array , do i need typecasting as well , like in Scanf ?
YoTaMiX 0 Light Poster
Hello ,
I wish to create a User-Defined array (1=float , 2=int) and to ALLOC
the array properly. I defined void* ARR in main function
and is a SWITCH (choise) i will make the desired array.
why doesnt it work?
Code is attached .
thanks :-)
#include<stdio.h>
#include<stdlib.h>
// void two_min(int(*comp)(void* a, void* b), void **answer, void **array);
int main()
{
int size_of_input=0;
int choise,loop;
void* user_array;
printf("Please Enter Type of Array to use : 1=Integer 2=Float");
scanf("%d",&choise);
switch (choise)
{
case 1 : { (*int)user_array=(*int)malloc(size_of_input*(sizeof(int)));
if (user_array==NULL) { printf("MEM ALLOC FAILED\n"); exit(1); }
else
{
printf("Enter Data to Array\n");
for (loop=0;loop<size_of_input;loop++)
scanf("%d",&user_array[loop]); }
}
case 2 : { user_array=(*float)malloc(size_of_input*(sizeof(float)));
if (user_array==NULL) { printf("MEM ALLOC FAILED\n"); exit(1); }
else
{
printf("Enter Data to Array\n");
for (loop=0;loop<size_of_input;loop++)
scanf("%f",&user_array[loop]); }
}
default : { printf("No such choise can be made , Exiting"); exit(1); break;}
}
return 0;
}
YoTaMiX 0 Light Poster
I already told you. Dont do this :
free(top); top=top->next;
Its very wrong !!!!!
And still you do it again !!!!
:mad:
First thing better change input by getche to fgets.
Do you think that this is nessary ???
if ( (temp==')') || (temp==']') || (temp=='}') )
{ printf("\n Bad Expression! Run it Again... \n");add to
case ')': { flag=check(temp[i]); } case ']': { flag=check(temp[i]); } case '}': { flag=check(temp[i]); }
break statemnts!
If the stack is not empty thats wrongif ( top ) { printf("Items on the stack\n"); flag = 0; }
You are ignoring the case which pop returns zero.
last thing
'(' - ')' == -1
A few things :
1. how do you Take off the Top each time if not in that way?
2. Added Break Statements , still causes problems in Expression check ,
each EXP check gets OK ....
3. the If sentence - no Math Exp. starts with backwoards brackets. :-)
4. should i pass into the POP function a Pointer to FLAG ?
Thanx
YoTaMiX 0 Light Poster
The logic should be like this : while ( input != 13 ) { if input is '(' or '{' or '[' or ')' or '}' or ']' { if input is '(' or '{' or '[' push it into the stack. else if input is ')' or '}' or ']' { if stack is empty report error. pop cell from stack if cell "matches input" we are ok. else if cell dont "matches input" or stack is empty report error. } } } if the stack is not empty report error.
Also I would suggest replacing your input function
with fgets.
A nice touch would be to add to the switch statment
a default case, checking if it is a number of math experssion.
Don't ever write code like this :free(top); top=top->next;
Once you free(p), dont access it !!!!!!!!!!
It will probaly work, but its wrong, and
also your grade will be like it.
I see your point :-)
i build something , but for some reason , it will state all Expression are OK and i tried to Debug it , and i cant figure what went wrong... Code Added . The check is though ASCII code :
'(' - ')' = 1
'' = 2
'{' - '}' = 2
Thanx
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Node
{ char data;
struct Node *next;
}node;
void push(char);
void display_stack();
node *top;
int main()
{
char temp[80];
int i=0,flag=1;
clrscr();
printf("\nEnter Math Expression\n");
top=NULL;
temp[i]=getche();
if ( (temp[i]==')') || (temp[i]==']') || (temp[i]=='}') )
{ printf("\n Bad Expression! Run it Again... \n"); getch(); return 1; }
else
{
while ( (int)temp[i]!=13 && (flag==1))
{
switch (temp[i])
{
case '(': { push(temp[i]); break; }
case '[': { push(temp[i]); break; }
case '{': { push(temp[i]); break; }
case ')': { flag=check(temp[i]); }
case ']': { flag=check(temp[i]); }
case '}': { flag=check(temp[i]); }
}
i+=1;
temp[i]=getche();
}
}
if (flag==1) printf("\nOK\n");
else printf("\nNO!\n");
return 0;
}
void push(char y)
{
node *ptr;
ptr=malloc(sizeof(node));
ptr->data = y;
ptr->next = top;
top = ptr;
}
void display_stack()
{
int i =0;
node * temp;
temp = top;
while(temp!=NULL)
{
printf("\nNode #%2d : Value= %c next %5x ",i++,temp->data,temp->next);
temp=temp->next;
}
}
/* REMOVES TOP NODE FROM THE STACK AND RETURNS ITS VALUE*/
char pop()
{
char a;
if(top==NULL)
{/* printf("\n\t\tSTACK EMPTY...\n\n");*/ return 0; }
else
{
a=top->data;
// printf("\n\n\nB4 pop: value 2B returned : %c ",a);
free(top);
top=top->next;
return a;
}
}
int check(char x)
{
char tmp;
tmp = pop();
switch (tmp)
{
case ')': { if( tmp-x!=1 ) return 0; break;}
case ']': { if( tmp-x!=2 ) return 0; break;}
case '}': { if( tmp-x!=2 ) return 0; break;}
}
return 1;
}
YoTaMiX 0 Light Poster
Hello to you all ,
I am required to write a program which gets a Math Expression (without checking it is good) and put in Stack only the Brackets .
Implementation of Stack is One-Way Linked List Only.
Examples for Good/Bad Expressions :
(a*{b+c}-4/x +[e-5]) - GOOD
(5+} - BAD
(5+{6*)-2} - BAD
(5+z - BAD
I have build up a Mechanism which checks for each entered char (other than Enter Key and also using closing brackets such as ) or ] or } at the beginning of Expression) what type of bracket it is and pushes it into a stack (we must use LIFO technique for check legal Expression).
I thought of 2 cases :
first case : for each open bracket there is a closing one so i check top of stack to bottom of stack and carry on till i reach to middle .
second case : what happens if i have this sequence - ()[]{} - meaning i need to check top against the next , and so on (top of stack to bottom of stack will not help in this case)
I kinda stuck on Algorithm Think Phase :-)
Code Added . Any Ideas ?
Thank you , Yotam , Israel
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Node
{ char data;
struct Node *next;
}node;
void push(char);
void display_stack();
node *top;
int main()
{
char temp[80];
int i=0,counter=0;
clrscr();
printf("\nEnter Math Expression\n");
top=NULL;
temp[i]=getche();
if ( (temp[i]==')') || (temp[i]==']') || (temp[i]=='}') )
{ printf("\n Bad Expression! Run it Again... \n"); getch(); return 1; }
else
{
while ( (int)temp[i]!=13 )
{
switch (temp[i])
{
case '(': { push(temp[i]); counter++; break; }
case ')': { push(temp[i]); counter++; break; }
case '[': { push(temp[i]); counter++; break; }
case ']': { push(temp[i]); counter++; break; }
case '{': { push(temp[i]); counter++; break; }
case '}': { push(temp[i]); counter++; break; }
}
++i;
temp[i]=getche();
}
}
if ( (counter%2)!=0 ) printf("Bad Expression! \n");
else { display_stack(); }
return 0;
}
void push(char y)
{
node *ptr;
ptr=malloc(sizeof(node));
ptr->data = y;
ptr->next = top;
top = ptr;
}
void display_stack()
{
int i =0;
node * temp;
temp = top;
while(temp!=NULL)
{
printf("\nNode #%2d : Value= %c next %5x ",i++,temp->data,temp->next);
temp=temp->next;
}
}
/* REMOVES TOP NODE FROM THE STACK AND RETURNS ITS VALUE*/
int pop()
{
char a;
if(top==NULL)
{ printf("\n\t\tSTACK EMPTY...\n\n"); return 0; }
else
{
a=top->data;
printf("\n\n\nB4 pop: value 2B returned : %c ",a);
free(top);
top=top->next;
return a;
}
}
YoTaMiX 0 Light Poster
Hello :-)
I have build a task of Linked List but for some reason , after i break the Input sequence by entering the same INT value twice in a row , the menu appears twice . howcome?
Thank , Code Attached .
Yotam
#include <stdio.h>
#include <stdlib.h>
typedef struct list_node
{
int data;
struct list_node *next;
}LIST_NODE,*LIST_NODE_PTR;
typedef struct mid_list
{
LIST_NODE* head;
LIST_NODE* end;
LIST_NODE* biggest;
int size;
}MID_LIST,*MID_LIST_PTR;
int add_value(MID_LIST_PTR list, int new_info);
int get_first_value(MID_LIST parm);
int get_last_value(MID_LIST parm);
int get_items_number(MID_LIST parm);
int the_biggest(MID_LIST parm);
void print_nodes(MID_LIST parm);
void menu();
int main()
{
char choise='1';
int input;
MID_LIST data_list;
MID_LIST_PTR data_list_ptr;
data_list.head=data_list.end=data_list.biggest=NULL;
data_list.size=0;
data_list_ptr=&data_list;
while (choise!='7')
{
menu();
scanf("%c",&choise);
switch(choise)
{
case '1': { printf("Please enter Data into Node \n");
scanf("%d",&input);
while (add_value(data_list_ptr,input)==1)
{
printf("Please enter Data into Node \n");
scanf("%d",&input);
}
break;
}
case '2': { printf("Printing Nodes \n"); print_nodes(data_list);break; }
case '3': { printf("Printing Last Node : %d \n",get_last_value(data_list));break; }
case '4': { printf("printing First Node : %d \n",get_first_value(data_list));break; }
case '5': { printf("Printing Biggest Node Value : %d \n",the_biggest(data_list));break; }
case '6': { printf("Printing Number of Nodes : %d \n",get_items_number(data_list));break; }
case '7': { printf("Now Exiting , thank you for using HW_4! \n"); exit(1);break; }
}
}
return 0;
}
void menu(void)
{
printf("Press 1 to add the node to the linked list\n");
printf("Press 2 to print the nodes\n");
printf("Press 3 for the last node\n");
printf("Press 4 for the first node\n");
printf("Press 5 for the biggest number\n");
printf("Press 6 for the number of nodes in list\n");
printf("Press 7 to exit\n");
}
int get_first_value(MID_LIST parm)
{
return parm.head->data;
}
int get_last_value(MID_LIST parm)
{
return parm.end->data;
}
int get_items_number(MID_LIST parm)
{
return parm.size;
}
int the_biggest(MID_LIST parm)
{
LIST_NODE* ptr=NULL;
int max;
ptr=parm.head;
max=ptr->data;
while (ptr!=NULL)
{
if (ptr->data>max) max=ptr->data;
ptr=ptr->next;
}
return max;
}
int add_value(MID_LIST_PTR list, int new_info)
{
LIST_NODE *temp;
temp=(LIST_NODE*)malloc(sizeof(LIST_NODE));
if (temp==NULL)
{
printf("\n Not enough memory for the allocation");
exit(1);
}
if (new_info!=list->end->data)
{
temp->data=new_info;
temp->next=NULL;
if (list->head==NULL) list->head=temp;
else list->end->next=temp;
list->end=temp;
list->size+=1;
return 1;
}
return 0;
}
void print_nodes(MID_LIST parm)
{
int i=1;
LIST_NODE* ptr=NULL;
ptr=parm.head;
while (ptr!=NULL)
{
printf("Node numder %d - data is %d : \n",i,ptr->data);
ptr=ptr->next;
i++;
}
}
YoTaMiX 0 Light Poster
that code is actually adding 1 in place of index ?
YoTaMiX 0 Light Poster
Hello :)
i need to write a program which gets from user number and index
( 0 <= number <= 255 , 0<=index<=7) .
then to add up to the number in bitwise a 1 in the place of the index :
if number is 102 which is 01100110
and index is 4 which is 01110110
and get 118 as a result.
for some reason it doesnt do that ..... i have no idea why , i used OR operation between numbers ..... :rolleyes:
Code added
thanx , Yotam
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void main()
{
unsigned char ch1,ch2;
int index,num,result;
printf("Please Enter an Unsigned Char and Index of Inclination\n");
scanf("%d%d",&num,&index);
if( (num<0)||(num>255)||(index<0)||(index>7) )
{
printf("error\n");
exit(1);
}
ch1=num;
ch2=pow(2,index+1);
result=ch1|ch2;
printf("\n %d",result);
}
YoTaMiX 0 Light Poster
Evening :)
I am trying to run a makefile on a program i wrote.
in Borland C it runs smoothly .
after running the makefile - all sorts of error appear which i do not understand.
i am adding the files (program + header + error DOC ) .
Makefile as written :
hw_2 : ex.o main.o
gcc -o hw_2 ex.o main.o
ex.o : hw2head.c hw2head.h
gcc -c -x c hw2head.c
main.o : hw2cpy.c hw2head.h
gcc -c -x c hw2cpy.c[
Thank you in advance ,
Yotam , Israel.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#include <conio.h>
#include <math.h>
#include "hw2head.h"
int main()
{
university univ={0};
char user_choice;
FILE *in,*out;
int num_of_std,flag_a=0,flag_b=0;
//clrscr();
univ.ptr=(student*)malloc(NUM *sizeof(student));
while (user_choice!='z')
{ user_choice=show_menu();
switch (user_choice) {
case 'a':
{
in=fopen("input.txt","r+");
if (in==NULL)
{
fprintf(stderr,"can't open file");
getch();
break;
}
num_of_std=make_file(in,&univ);
fclose(in);
printf("finished kelet\n");
flag_a=1;
break;
}
case 'b':
{
if ((out=fopen("output.txt","wt"))==NULL)
{
fprintf(stderr,"can't open file");
getch();
break;
}
if ((flag_a!=1) || (flag_b==1))
{
fprintf(out,"Not Available");
printf("You Didn't Select In The Right Order\n");
getch();
exit(1);
}
write_file(out,&univ,num_of_std);
flag_b=1;
fclose(out);
printf("finished writing file , i am having a coffee break \n");
break;
}
case 'c':
{
if ((in=fopen("output.txt","w+"))==NULL)
{
fprintf(stderr,"can't open file");
getch();
exit(1);
}
if ((flag_a!=1) || (flag_b!=1))
{
fprintf(out,"Not Available");
printf("You Didn't Select In The Right Order\n");
getch();
exit(1);
}
grade_factor(in,&univ,num_of_std);
fclose(in);
printf("finished factoring grades \n");
break;
}
case 'z':
{
out=fopen("output.txt","w+");
fprintf(out,"END_OF_PROGRAM");
fclose(out);
printf("Program Terminated\n");
getch();
exit(1);
}
}
flushall();
}
realloc(univ.ptr,0);
clrscr();
return 0;
}
// FUNCTIONS //
char* grade_bit; //Global Pointer , saving Grades after HW check
char show_menu()
{
char key_strike;
printf("Welcome to University system \nChoose Operation :\n");
printf(" 'a' - Create File in Current Path and start Input \n");
printf(" 'b' - Create File in Current Path according to HW submission \n");
printf(" 'c' - Show Factored Grades of students \n");
printf(" 'z' - Exit Program and have a cup of coffee \n\n\n\n\n" );
key_strike=getchar();
return key_strike;
}
int make_file(FILE *in,university *un)
{
char buff[BUFSIZ];
int count=0,i;
while ( fgets( buff, sizeof(buff), in ) != NULL ) {
student s;
if ( sscanf( buff, "%s %ld %f %s",
s.name, &s.id, &s.avg, s.hw_submit ) == 4 ) {
/* success decode, extend array and copy the info */
student *temp = (student*)realloc( un->ptr, (count+1)*sizeof(student) );
if ( temp != NULL ) {
un->ptr = temp; /* update array */
un->ptr[count] = s; /* copy data */
count++; /* one more stored */
} else {
/* no more room, return with what we have */
return count;
}
} else {
/* that line didn't make sense, report it */
fprintf( stderr, "Bad line %s", buff );
}
}
return count;
}
void write_file(FILE *out,university *un,int num)
{
int i,j,count;
char grade;
grade_bit=(char*)malloc(num*sizeof(char));
if (grade_bit==NULL)
{ printf("No Memory for new grades , now EXIT \n"); getch(); exit(1); }
for(i=0;i<num;i++)
{
count=0; grade='0';
fprintf(out,"student : %s %ld %.2f ",un->ptr[i].name,un->ptr[i].id,un->ptr[i].avg);
for (j=0;j<5;j++)
{
if ((char)un->ptr[i].hw_submit[j]=='1') count+=1;
}
if (count<3) fprintf(out,"%c",grade);
else { grade='1' ; fprintf(out,"%c",grade); }
grade_bit[i]=grade;
fprintf(out,"\n");
}
}
void grade_factor(FILE* in,university *un,int num)
{
float max_grade;
int i;
max_grade=un->ptr[0].avg;
for (i=1;i<num;i++)
{
if (un->ptr[i].avg>max_grade)
{
max_grade=un->ptr[i].avg;
}
}
for (i=0;i<num;i++)
{
fprintf(in,"student : %s %ld ",un->ptr[i].name,un->ptr[i].id);
if (un->ptr[i].avg==max_grade) un->ptr[i].avg=100;
else un->ptr[i].avg=((un->ptr[i].avg)*100)/max_grade;
fprintf(in,"%.2f %c \n",un->ptr[i].avg,grade_bit[i]);
}
free(grade_bit);
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#define NUM 5
typedef struct { // Student Structure //
char name[15];
long id;
float avg;
unsigned char hw_submit[5];
} student;
typedef struct { //University Structure //
student* ptr;
} university;
static void forcefloat (float *p) /* fixing scanf floating point */
{
float f = *p;
forcefloat(&f);
}
char show_menu(); //Menu Function
int make_file(FILE *in,university *un); // Reading Input File
void grade_factor(FILE* in,university *un,int num); //Re-Writing after Factor Grades
void write_file(FILE *out,university *un,int num); // Writing File after HW check
This attachment is potentially unsafe to open. It may be an executable that is capable of making changes to your file system, or it may require specific software to open. Use caution and only open this attachment if you are comfortable working with msword files.
YoTaMiX 0 Light Poster
In function input_university() I see where scanf() is reading the string, and univ->std[num_cells].name is getting allocated, but I don't see where it is copying buffer to univ->std[num_cells].name after allocation. Add this and it will probably work ok.
strcpy(univ->std[num_cells].name,buffer);
I did just that . now it messes up another string - i'll figure it out
somehow , the thing is , we were never tought the subject BUFSIZ and
"sscanf" - so no offence there ;)
i got slapped on using it .... sorry
YoTaMiX 0 Light Poster
Apparently, I wasted my time :mad:
http://www.daniweb.com/techtalkforums/thread43435.htmlNot only have you dropped the initialisation of univ in main(), but you've gone back to using fscanf to read the file.
Not only that, you're now using global variables like "temp", which get allocated on every iteration of the loop and never freed.
Don't forget the not using a temporary variable when calling realloc, nor the unnecessary casting of malloc and realloc.Y'know what, if you're just going to ignore people, just say so - ok?
I guess i didnt see it - sorry mate :confused:
YoTaMiX 0 Light Poster
Hello to you all ,
Major problem in C , Files included
Summary of problem :
Program will not copy the feild "name" from struct "univ->std.name" properly. the rest of data is presented fairly.
do not touch function "output_university" .
Thank you ,
yotam , Israel .
Nir 32251 99.80 11001
Ely 12347 77.89 01111
Moshe 45321 50.34 11111
Avi 31456 49.78 00011
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include <math.h>
#define MAX 20
static void forcefloat(float *p)
{
float f=*p;
forcefloat(&f);
}
typedef struct{
char* name;
long id;
float avrg;
char works[5];
}student;
typedef struct{
student *std;
}university;
void input_university(university *univ, FILE *in);
void output_university(university *univ, FILE *in);
void output_factor(university *univ, FILE *in);
int num_cells=0;
char* temp;
int main()
{
FILE *in, *out;
university univ;
char op='a';
static int flag=0;
printf("please enter your options:\n");
printf(" a:for reading\n b:for output of the student information\n c:for showing marks after the factor\n z:exit");
while(op!='z')
{
scanf("%c",&op);
switch(op)
{
case 'a':if((in=fopen("input.txt","r"))==NULL)
{
fprintf(stderr, "input file not available");
break;
}
else {
input_university(&univ,in);
fclose(in);
flag=1;
break; }
case 'b':if(flag==1)
{
if((out=fopen("output.txt","w"))==NULL)
{
fprintf(stderr, "output file not available");
break;
}
else {
output_university(&univ,out);
fclose(out);
flag=2; }
}
else
{
if((out=fopen("output.txt","w"))==NULL)
{
fprintf(stderr, "output file not available");
break;
}
fprintf(out, "'b' before 'a'-not available");
fclose(out);
break;
}
break;
case 'c': if(flag==2)
{
if((out=fopen("output.txt","a"))==NULL)
{
fprintf(stderr, "output file not available");
break;
}
else {
output_factor(&univ,out);
fclose(out); }
}
else{
if((out=fopen("output.txt","a"))==NULL)
{
fprintf(stderr, "output file not available\n");
break;
}
fprintf(out, "'c' brfore 'a' or 'b' -not available\n");
fclose(out);
break;
}
break;
case 'z': { printf("end of program"); }
}
}
getch(); flushall();
return 0;
}
void input_university(university *univ, FILE *in)
{
int i,j,str_s;
char buffer[MAX];
while(fscanf(in,"%s",buffer)!=EOF)
{
str_s=strlen(buffer);
univ->std=(student*)realloc(univ->std,(num_cells+1)*sizeof(student));
if (univ->std==NULL) exit(1);
univ->std[num_cells].name=(char*)malloc(str_s+1);
if (univ->std[num_cells].name==NULL) exit(1);
temp=(char*)malloc(5*sizeof(char));
if (temp==NULL) exit(1);
fscanf(in,"%ld",&univ->std[num_cells].id);
fscanf(in,"%f",&univ->std[num_cells].avrg);
strcpy(temp,univ->std[num_cells].works);
fscanf(in,"%s",temp);
printf("%s %ld %.2f %s \n",univ->std[num_cells].name,univ->std[num_cells].id,univ->std[num_cells].avrg,univ->std->works);
++num_cells;
}
}
void output_university(university *univ, FILE *out)
{
int i,j,count;
char *temp;
fprintf(out,"BEFORE:\n");
for(i=0;i<num_cells;i++)
{
fprintf(out,"student %d : ",i);
//fputs(univ->std[i].name,out);
fprintf(out,"%s",univ->std[i].name);
fprintf(out," %ld",univ->std[i].id);
fprintf(out," %.2f",univ->std[i].avrg);
count=0;
temp=univ->std[i].works;
while(temp!='\0'){
if(*temp==1)
count++;
temp++;
}
if(count==3 || count>3)
fprintf(out,"%s",'1');
else
fprintf(out,"%s",'0');
fprintf(out,"\n");
}
}
void output_factor(university *univ, FILE *out)
{
int i,j,max=univ->std[0].avrg,factor,count;
char *temp;
fprintf(out,"AFTER:\n");
for(i=1;i<num_cells;i++)
{
if(univ->std[i].avrg>max)
max=univ->std[i].avrg;
}
for(i=0;i<num_cells;i++)
{
fprintf(out,"student %d\n",i);
fputs(univ->std->name,out);
fprintf(out,"%d",univ->std->id);
factor=((univ->std[i].avrg) *100 )/max;
fprintf(out,"%f",factor);
}
temp=univ->std[i].works ;
count=0;
while(temp!='\0'){
if(*temp==1)
count++;
temp++;
}
if(count==3 || count>3)
fprintf(out,"%s",'1');
else
fprintf(out,"%s",'0');
}
YoTaMiX 0 Light Poster
i ment - i can't figure it :) LOL
YoTaMiX 0 Light Poster
Hello to you all ,
I have a program who i want to copy one input.txt to a output.txt
and for some reason it duplicates the string and it doest create a full duplicate .
i am over this for a few hours now and i can figure it out.
can you help me?
Input file and source is attached , neutrilze the Z option in the Menu
thanx :)
Nir 32251 99.80 '11001'
Ely 12347 77.89 '01111'
Moshe 45321 50.34 '11111'
Avi 31456 49.78 '00011'
YoTaMiX 0 Light Poster
> void main
main returns int> char user_choice;
> while (user_choice!='z')
This variable is uninitialised at the point you first use it.> university univ;
This isn't initialised either.
Which is very important when you get to make_file(), since you do
- dereference an uninitialised pointer
- try and realloc an uninitialised pointer> if (un->ptr==NULL) un->ptr=(student*)realloc(un->ptr,(count+1)*sizeof(student));
The test serves no purpose, since at best it only extends the array once.In main(), you need this to start off with a NULL pointer
university univ = { 0 };
And this function needs to be something likeint make_file(FILE *in,university *un) { char buff[BUFSIZ]; int count=0; while ( fgets( buff, sizeof buff, in ) != NULL ) { student s; if ( sscanf( buff, "%s %ld %f %ld", s.name, &s.id, &s.avg, &s.hw_submit ) == 4 ) { /* success decode, extend array and copy the info */ void *temp = realloc( un->ptr, (count+1)*sizeof(student) ); if ( temp != NULL ) { un->ptr = temp; /* update array */ un->ptr[count] = s; /* copy data */ count++; /* one more stored */ } else { /* no more room, return with what we have */ return count; } } else { /* that line didn't make sense, report it */ fprintf( stderr, "Bad line %s", buff ); } } return count; }
I did so , however , only the first name was fixed .... how bizzare.
1. what does "buff" …
YoTaMiX 0 Light Poster
Hello to you all ,
I am having a little trouble in C , working with Files.
for some reason , it doesnt copy to output file the first letter and jumps
over 3 digits of the last number in the file .
whats wrong , i cant find it .
yotam
P.S - source and Input.txt included
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
typedef struct { // Student Structure //
char name[15];
long id;
float avg;
long hw_submit;
} student;
typedef struct { //University Structure //
student* ptr;
} university;
static void forcefloat (float *p) /* fixing scanf floating point */
{
float f = *p;
forcefloat(&f);
}
char show_menu();
int make_file(FILE *in,university *un);
void write_file(FILE *out,university *un,int num);
void main()
{
university univ;
char user_choice;
FILE *in,*out;
int num_of_std;
while (user_choice!='z') {
user_choice=show_menu();
printf("chosen Key is %c \n",user_choice);
switch (user_choice) {
case 'a':
{
in=fopen("input.txt","r+");
if (in==NULL)
{
fprintf(stderr,"can't open file");
getch();
break;
}
num_of_std=make_file(in,&univ);
fclose(in);
printf("finished kelet\n");
break;
}
case 'b':
{
if ((out=fopen("output.txt","wt"))==NULL)
{
fprintf(stderr,"can't open file");
getch();
break;
}
write_file(out,&univ,num_of_std);
fclose(out);
printf("finished writing file , i am having a coffee break \n");
break;
}
}
}
}
// FUNCTIONS //
char show_menu()
{
char key_strike;
printf("Welcome to University system \nChoose Operation :\n");
printf(" 'a' - Create File in Current Path and start Input \n");
printf(" 'b' - Create File in Current Path according to HW submission \n");
printf(" 'c' - Show Factored Grades of students \n");
printf(" 'z' - Exit Program and have a cup of coffee \n\n\n\n\n" );
key_strike=getchar();
return key_strike;
}
int make_file(FILE *in,university *un)
{
int i=0,count=0;
while (getc(in)!=EOF)
{
fscanf(in,"%s",un->ptr[i].name);
fscanf(in,"%ld",&un->ptr[i].id);
fscanf(in,"%f",&un->ptr[i].avg);
fscanf(in,"%ld",&un->ptr[i].hw_submit);
count++;
i++;
if (un->ptr==NULL) un->ptr=(student*)realloc(un->ptr,(count+1)*sizeof(student));
}
return count;
}
void write_file(FILE *out,university *un,int num)
{
int i;
for(i=0;i<num;i++)
fprintf(out,"student : %s %ld %.2f %ld\n",un->ptr[i].name,un->ptr[i].id,un->ptr[i].avg,un->ptr[i].hw_submit);
}
Nir 32251 99.80 11001
Ely 12347 77.89 01111
Moshe 45321 50.34 11111
Avi 31456 49.78 00011
YoTaMiX 0 Light Poster
thanx , that will suffice :-) cheers!
YoTaMiX 0 Light Poster
its foolish that in the function every time u r initializing i with 0 and returning home if null or not moving further because u still are with i=0 in the remaining iterations also.
solution to ur code is declare ur int as static int.
then the problem is solvedget help at >>> prof.thakur@yahoo.co.in
I am putting up the code in attachment.... i tried watching the process and i still cant find whats wrong
thanx :D
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
int func(char *s1,char *s2);
char* str_input(char *temp); //Dynamic STR creator
void main()
{
char *str1,*str2;
int sum=0;
strcpy(str1,str_input(str1));
puts(str1);
strcpy(str2,str_input(str2));
puts(str2);
sum=func(str1,str2);
printf("sum is %d",sum);
}
char* str_input(char *temp)
{
char line_size[80];
static int i=0;
printf("Enter String No. %d \n",++i);
gets(line_size);
temp=(char*)malloc((strlen((line_size)+1)*sizeof(char)));
if (temp==NULL)
{
printf("No Memory for STR , Press a Key to EXIT");
getch();
exit(1);
}
else strcpy(temp,line_size);
}
int func(char *s1,char *s2)
{
static int place=0;
if ((s1!=s2) || (s1==NULL) || (s2==NULL)) return 0;
return 1+func(s1++,s2++);
}
YoTaMiX 0 Light Poster
Code :
int func(char s1[], char s2[]) { int i=0; if (s1[i]!=s2[i]) return 0; if (s1[i]==s2[i]) return 1+func(s1[i+1],s2[i+1]); }
/QUOTE]
>>if (s1!=s2) return 0;
here think what happens if the first character does not match
does it remain the type of function you want..
and you need sum kind of parameter checking to end the recursive loop
add the condition of some kindsanket
the first IF is the stop condition for loop , if first letter doesnt match , it will not run on the rest of the strings. no?
yotam
YoTaMiX 0 Light Poster
Hello to you all ,
I have been assigned to in C to build a function which will take 2 strings and count (in recursive way) how many chars are alike (case sensitive).
I have setup the main and also a function which recieves the input from user .
I have a problem converting the theory to commands :
if the function recieves 2 strings , the comparison needs to letter to letter with the same index , and adding 1 to the returned value if they are equal, in which the advance down the recursive process is "index+1".
I tried it , but it doesnt work and i have no idea howcome .
Code :
int func(char s1[], char s2[])
{
int i=0;
if (s1!=s2) return 0;
if (s1==s2) return 1+func(s1[i+1],s2[i+1]);
}
am i missing something ?
thank you
Yotam :D
YoTaMiX 0 Light Poster
where is it located , the compiler in the Fedora System .... i cant find it
YoTaMiX 0 Light Poster
Hello :-)
I have installed Fedora Core 4 and we learn in college C++ .
where can i find a C++ author and compiler for Fedora Core 4?
Thank you
YoTaMiX 0 Light Poster
Hello ,
I wish to Duplicate certain Fields in the same tables when i open a new form that is related to that same table .
If i create a button of Duplicate Record , he will duplicate the Primary Key - which i do not want to "Duplicate OK" .
Thanx , Yotam , Israel
YoTaMiX 0 Light Poster
Hello to you all ,
i have recieved an assignment to build a recursive function
which will recieve an array which has to check if it is "Up&Down" from both sides : Example : 2 5 6 12 7 5 2 .
according to the specs of the H.W assignment , i need to find an "extreme value" (in example - 12) .
how should i approach this?
Thank you
Yotam
YoTaMiX 0 Light Poster
Hello to you All ,
For some reason , the Compiler throws me an Error
"Illegal Operation" on the Comparison described below :
#include <stdio.h>
#include <math.h>
#include <conio.h>
typedef struct {
double x,y;
} Point;
typedef struct {
Point l, r;
} Rectangle;
typedef struct {
Point Center;
double r;
} Circle;
int main()
{
Circle user_c;
Rectangle user_r;
Point user_p;
printf("Please Enter Upper Right Corner , Lower Left Corner , Point X and Y\n");
scanf("%ld%ld%ld%ld",&user_r.r,&user_r.l,&user_p.x,&user_p.y);
if ( ((user_p.x) > (user_r.r)) && ( (user_p.x) < (user_r.l)) )
printf("OK");
return 0;
}
YoTaMiX 0 Light Poster
Thanx , i hope i can use it :-)
YoTaMiX 0 Light Poster
Hello again ,
I am adding the Code for the Prog .... where the remarks are,
its is where i stuck . thanx again
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void ResetArray (int **input,int a_size);
void PrintArray (int **input,int a_size);
void swap(int **a, int **b);
void sankesort(int **input,int a_size);
void main()
{
int **user,i,x,size;
randomize();
printf("enter Dimension\n");
scanf("%d",&size);
// Memory Allocation for 2D Array
user =(int**) malloc(sizeof(int *) * size);
for(x = 0; x < size; x ++)
{
user[x] =(int*) malloc(sizeof(int) * size);
}
ResetArray(user,size);
PrintArray(user,size);
printf("\n");
snakesort(user,size); //No idea what to do here
PrintArray(user,size);
//End of Program (Thank God)
getch();
free(user);
}
//Functions (Top Down Design)
void ResetArray (int **input,int a_size)
{
int i,x;
for (i=0;i<a_size;i++)
for(x = 0; x < a_size ; x ++)
{
*((input[x])+i) =100+random(101);
}
}
void PrintArray (int **input,int a_size)
{
int i,x;
for (i=0;i<a_size;i++)
{
for(x = 0; x < a_size ; x ++)
{
printf("%d ",*((input[x])+i));
}
printf("\n");
}
}
void swap(int **a, int **b)
{
int temp = **a;
**a = **b;
**b = temp;
}
// what to do here ...
void sankesort(int **input,int a_size)
{
}
YoTaMiX 0 Light Poster
question : how Qsort will help me? if a 2D array is one big 1D arrray , should i work line by line ?
YoTaMiX 0 Light Poster
Hello you all!!!
I got an Assignment to sort a Randomized Dynamic 2D Matrix of Integers , Snake style : for Example : biggest number in row 0 , after it follows (beneath it , vividly) the next number :
1 2 3 4 5
10 9 8 7 6
11 12 13 14 15
20 19 18 17 16
21 22 23 24 25
(after Sort for example) .
it has to be as less time complex as possible.... and i am stuck. Thanx a lot