Print the value of coming in from ch
Print the value of coming in from ch
1 is kind of possible .... But it is messy
Check out this piece of code
#include<stdio.h>
#include<stdlib.h>
struct student
{
void *subjects[15];
};
int main() {
struct student s1;
int numberOfSubjects;
int i =0;
printf("How main subject does the student have \n");
scanf("%d",&numberOfSubjects);
for(i=0;i< numberOfSubjects;i++) {
s1.subjects[i] = (int *)malloc(sizeof(int) *1);
}
printf("Now enter the marks\n");
for(i=0;i<numberOfSubjects;i++){
printf("Subject %d \n",i);
scanf("%d",(int *)s1.subjects[i]);
}
printf("****************************");
printf("The entered marks are \n");
for(i=0;i<numberOfSubjects;i++){
printf("Subject number:%d -----%d\n",i,*(int *)s1.subjects[i]);
}
printf("\n");
return 0;
}
In your code the declaration of the pointer account_array is fine.
char (account_array[size];
I guess you meant
char *account_array[size];
I ran the code you had provided and it gave an error
$ ./test
ping: invalid option -- 'D'
Usage: ping [-LRUbdfnqrvVaA] [-c count] [-i interval] [-w deadline]
[-p pattern] [-s packetsize] [-t ttl] [-I interface or address]
[-M mtu discovery hint] [-S sndbuf]
[ -T timestamp option ] [ -Q tos ] [hop1 ...] destination
obtained the following seed: 0.0
$
Also how about clearing the content in the namelist array before using the add function
for(int i=0;i<size;i++)
{
for(int j=0;j<namelist[i].length;j++)
namelist[i][j] = '\0';
}
Maybe overkill but wont hurt
Don't use the ping command.
Create a socket to a remote server. Write some data to the socket.Check how much time it takes for the server to respond.
The time difference can be your seed
In the function to calculate variance is
(a) Average being calculated correctly ?
(b) Can you please use brackets to more explicitly specify the order of the arithmetic operations
When you write
variance = (double)sum_sqrs / (double)n - (average)*(average);
It will
(1) divide sum_sqrs by n
(2) multiple average by average
Subtract (2) from (1)
Is this what you want ?
Can you please post the relevant code ? I cannot figure out what you are trying to say
Yes ...
An uninitialized global variable takes the value 0. Doing a 0/0 division will give you an Arithmetic exception. I think this should solve the crashing issue.
How did you compile the program ? I also got the same compilation errors that mitrmkar got
In the function to calculate the median you have used the variable i without initializing it.
Hi Chester I have a vague idea of what your question is , but before I dispense any advise can you please clarify your question ?
PS: Some random thoughts which may or may not help you
1. If you have an array of structs, there is no way to set arr[index] = null. What you can do is add a member to the struct definition called as isValid. Later if you want to set arr[index] = null, you can instead set arr[index].isValid = 0. In your print functions you can ignore all arr[index] where isValid is set to 1
2. If you have 2 struct variables t1 and t2 then if you write t1 = t2 , all of t1's contents will be replaced by t2 contents
Hope this helps
I think Dev C++ is usually for C++ not C thought I am not sure
Please correct me if I am wrong
This is how I would approach this problem
Suppose I have to remove the string "tes" from United States of America
1. Check to see if I can find a t any where in United states of America
2. If do find a t then check for e and then s
3. Once I find the start index of the match, in this case 10 the end index has to be 13
4. Then from index 14 till the end of the string I copy chars one by one to positions 10,11,12 ....
I guess the tricky part of this exercise is identifying when you have a string match. Check out this algorithm. Might be of help
Dont use fscanf use fread .
Read the input into a char buffer . Then use string processing to extract the information you need
You could take the hash map route
Is the format of the input file pre defined or can it change
If the input file format is not fixed then this is a hard problem
atoi is used with string and not chars. This link explain the function along with some sample code
just google for malloc/ calloc you will find tons of documentation and examples
When you are reading from the file the value of MAX is 101. But each line has less than 101 characters . So what you want to do is something of this sort
int
main()
{
FILE *fp = fopen("mytestfile.txt","r");
char arr[100]; // Open a file
char *p;
if(fp == NULL)
{
printf("File does not exist");
return -1;
}
while(fgets(arr,100,fp)!=NULL) // Read from the file
{ // Assume each line will have less than 100 character
p = strchr(arr,'\n'); //Each line will be terminated by \n. Find the position of the \n
arr[p - arr] = '\0'; // Replace the \n by \0
printf("%s\n",arr);
strcpy(arr,"\0"); // Clear the array
}
fclose(fp);
return 0;
}
Simplest solutions: Make the variable global . So you want to write some thing of this sort
char *p = NULL;
void func()
{
p= "Value returned using global variables";
}
int
main(int argc, char *argv[])
{
func();
printf("%s\n",p);
return 0;
}
Or you can return a char array from a function. Then you are writing code of this sort
char* func()
{
char *p = (char *)malloc(sizeof(char)*10);
p[0]='A';
p[1]= 'B';
p[2]= 'C';
return p;
}
int
main(int argc, char *argv[])
{
char* p =func();
printf("%s\n",p);
free(p);
return 0;
}
I can help you a little bit. My changes are in red
show you simple example of pipe
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <wait.h> int main() { pid_t pid; int pipe_fd[2]; if ( pipe(pipe_fd) == -1 ) { perror("Pipe create error"); exit(1); } if ( (pid = fork()) == -1 ) { perror("Fork create error"); exit(1); } if ( pid == 0 ) { close(pipe_fd[0]); /* close open fd of pipe */ /* write fd of pipe */ /* Connect stdout to your pipe */ /* Then execute the grep function */ if ( write(pipe_fd[1], "this is pipe", 128) == -1 ) { perror("Write Error"); exit(1); } exit(0); } if ( pid > 0 ) { waitpid(pid, NULL, 0); close(pipe_fd[1]); /* close write fd of pipe */ char buffer[14] = { 0 }; /* read fd of pipe */ /* Now execute the sort command. But I dont know how will you tell the system that you want to sort the contents in buffer */ if ( read(pipe_fd[0], buffer, 128) == -1 ) { perror("Read error"); exit(1); } printf("Read string is : %s", buffer); exit(0); } }
By the way how is the book ?
As in what topics does the book cover ?
This is my O/P on a mac machine
~$ ./test
initial file handle :: 0
child read :: abcdefghij
child file handle :: 11
parent file handle :: 28
parent read ::
end file handle :: 28
~$
Maybe some one could shed some more light on this matter
and i want to know is it legal to have one side pointer arithmetic and on one side a int variable
I guess what you are asking is, is it legal to have a pointer and an integer value in the same arithmetic expression
You can have something of this sort
int x[]={1,2,3,4,5};
int *p=x;
printf("%d\n",*p); // Prints out 1
p=p+1;
printf("%d\n",*p); // Prints out 2
@daredevil786
The most simplest explanation for you code
y= ptr- x;
= (x+4)- x;
= 4
On a more serious note, have you studied pointer arithmetic ?
There are a couple of things
If you have
int x=[1,2,3,4,5];
printf("%u\n",x); // Address of the first element of the arry
printf("%d\n",*x); // Value of the element at the address x ie the first element of the array
So if you write the statement
y = ptr - *x;
// ptr contains an address, but *x contains a value
Also I think you made a typo in the line
int *ptr, y;
// Should have been
int *ptr, *y;
It appears line 6 needs to be changed as well, to the following:
int *nums = new int[SIZE];
It seems windows doesn't like to initialize such large arrays the way you were using, but this way will work fine.
This is a C forum ..... the operator new wont work with a C compiler
What I mean try getting
the 2 prime number then
the 4 prime number then
the 8 prime number then
the 16 prime number then
the 32 prime number ....
See at which point your code fails
Did you try to go the exponential way ?
I ran your code and I got the correct o/p ie 10,4743
Where are you running your code ? Is it a specialized device ?
One thing you could do is dont jump straight to 10,000 Go there exponentially you know first calculate the 2,4,8,16,32,64 and so on
im not really good in understanding, perharps posting the skeleton code might help? thanks
I have written the names of the functions that you need to use. If you search for each function you will get a ton of information.
Adding a little bit more information to gerard4143's post
Open the file (Use fopen for this)
Read the first line of the file and store it in a buffer (Use fread for this)
Go through the line and check if the name of the country appears in the file or not (Use strtok for this)
If a match is found send the contents of the line you just read to the client or send error
Have you heard of a little known website called as google.... Try it out.... You might like it
Here is what I found in just 0.23 seconds in this website
What have you got so far ?
Here is some thing to get you started
http://www.cs.cf.ac.uk/Dave/C/OLDC/subsection2_18_4_1.html
Can you paste the content of the input file ?
Also I do not understand the rationale behind the number 80 ?Can some one explain why the size of the temp buffer is 80
You are closing the file, the first time you enter the else statement block. So the next time you come to the else block you will not be able to write anything to the file
Instead close the file when you come out of the while loop
I think the type cast has to be done before applying the shift operator. Here is an example to show why
int main()
{
float f= 5.67;
int x= (int)f<<2;
printf("%d\n",x);
return 0;
}
If f is not typecasted before applying the shift operator then the code will not compile as shift operators cannot be applied to floating point numbers.
This code does compile and produces the answer 20
Just think for a moment what you are doing.
In line 2 you are asking the computer to store a string in a char. So if I give the input as ABCDEFGH it will be stored in a char
Then in line 3 you are telling the computer to store that char in an array .
Is this what you really want ?
Initial array is 1,2,3,4,5,6,7,8,9,10
After rotating it 3 times 8,9,10,1,2,3,4,5,6,7
Well to take input from the user there are many ways.
The easiest one is through command line arguments
The next one is through scanf() function. But this function is very dangerous, has lot of pitfalls. So you should avoid using it
Another way of taking user input is to use fgets. This is the safest way, but it is slightly more complicated
I do not think that your program is crashing....
This is what happens when you enter a char other than c.
Since the char is not c, the code goes to the default case, prints "press c to continue" then comes out of the switch scenario and wait for you to enter a char in response to getch()
If you want to give the user another choice to enter the char, you have to put the switch inside a while loop
The return value for the read and write system calls is the number of char successfully read/ written.
Print that value and post the result here
This is not a simple program.
First step would be to i/p and then o/p a string.
Then you should see if you can count the length of the string
My favorite book for C is "Let Us C" by Yashwant kanitkar
It teach C from the very basic and makes no assumption about the programming capability of the reader
You could pass the input string as a command line argument
int main(int argc,char* argv[])
{
printf("%s\n",argv[1]);
}
and then when you execute the program, you execute it like this ./test input string
I seriously think you need to read C from a book.
According to your above post the function set_number_people is not defined to be a part of any class but yet when you call it, you use an object to call the function
I think you need to do a bit of reading on C
Do you have any books, tutorials on C ?
If not there are some links available in this forum
Yes take i/p in the char array instead of the string. The for the algorithm you want something of this sort
char arr[]= "input string"
char a= arr[0]
int count =1
loop i from 1 to the length of the string
if arr is equal to a then increase count
else
{
print a, count
a= a
count= 1
}
Did you get the algorithm ?