When you just type ls, your code is breaks out at line 79. Check the usage of strtok properly
abhimanipal 91 Master Poster
When you just type ls, your code is breaks out at line 79. Check the usage of strtok properly
Can you tell me where did you write the print function ? I wrote the print function in the main after the sort function was called and I was able to print the array elements just fine.
Here is another idea... It will probably get you started but you have to do more work on it to get the finish product
void display(int n)
{
// Right now all the X will be left justified. You have to figure out how to include spaces.
int i=0,j=1,num=1;
printf("In display %d\n",n);
for(i=1;i<=n;i++)
{
while(j<=2*i-1)
{
// Fill this part
}
j=1;
printf("\n");
}
}
int main(int argc, char* argv[])
{
int i=0,num=1,num1=2,toggle=0;
//Assume that user input is 3
for(i=0;i<=3;i++)
{
if(toggle==0)
{
display(num);
num=num+2;
toggle=1;
}
else if(toggle==1)
{
// Similarly write this one
}
}
return 1;
}
Well suppose after the first sort, this is your input array
int arr[10]={1,2,3,7,8};
if the user enters 6, all you got to do is move the elements 8,7 one step behind. You see 8 is greater than 6 copy 8 to the 6th location. Same applies for 7. 3 is not greater than 6, so the loop ends there
Your array size is 5 and you want to continuously read the user input till the user exits, how will there be sufficient space in the array ?
You cannot write
username == 'udental'
Use strcmp instead
Will your input always be a square matrix ? Or the way you are calculating the number of columns is wrong.
Read a line using fgets, then copy that line character by character at the appropriate location in the matrix.
Do a google search
Hint ad i++ after the search string... for obvious reasons
Hello People,
I just came across this code as I was browsing through some C++ problem sets. This code gives compile time error but I am unable to understand the reason. I am C guy and my knowledge about C++ is not that great. So pardon if there is an obvious solution to this problem. Any help would be appreciated
class Base
{
public:
Base();
virtual ~Base();
};
class derived: public Base
{
public:
virtual ~derived();
};
int main()
{
Base *pb = new derived;
getch();
}
Check out this link. This will show you a better method to read an array.
http://irc.essex.ac.uk/www.iota-six.co.uk/c/i2_feof_fgets_fread.asp
Define a 2D array of chars (or 1D array of char pointers)
As you read a line of the file store it in a row of this 2D array
So at the end of the loop you will have a 2D array in which each row contains one line of the input file
Google Masters theorem .....
I am sorry but I do not understand why does this algorithm not solve the problem....
Can you give some sample input where the algorithm will fail ?
I think my algorithm has complexity of O(n).
The size of the temp buffer is n . It takes time O(n) to build this temp buffer and time O(n) to scan across this buffer.
O(n) + O(n) = O(n)
unless I am missing some thing
There are 2-3 mistakes in your code
1. You are storing the id is a[0][0], yet when you start storing the marks of the students you start again from a[0][0], the index of the loop on line 23 should start from 1. This mistake is repeated again in the function printinfo.
2. In the function printinfo(), the code for adding all the grades is wrong. Your sum statement is basically adding a[1][6] 5 times. See how you printed the array and compare how you are adding them
3. You have made the same functions in a couple of place is the findinfo function as well
Check out this link for more information... The problem being discussed here is pretty similar to yours
What exactly do you mean by different from all numbers ??? Does it mean that the array has contains many elements with many duplicates, but one element has only 1 copy ?
Make a 2-D array temp of size n. This array will store the element and the number of times this element has occurred
Initialize the temp array to 0
Scan through the input array. If you get an element say 10,
temp.element=10
temp.count++;
i++
Scan the temp array.
If temp.count is 0, then the element i occurs just once in the input array
else the element i occurs more than once
Run time of this algo is O(n). Space complexity is O(2n)
The objective of BFS is to print all the nodes at a given level, then move to the next level
In DFS we go depth wise as far as we can go. Then when we cant go any further we backtrack and then try again and so on.
DFS gives you a forest because you have many connected components as opposed to BFS where you have only 1 connected component. (Read the definition of connectivity for better understanding of this)
Move char* temp to the top of the function sort. Right after the line 67
and make it
char* temp=0;
Its a good programming practice to initialize all variables to 0. Helps to reduce errors
Damm.... I feel like such an ass .....:@
Hello People,
I cannot understand how this expression gets evaluated ..
1+~0
From what I know ~ has higher priority . So the expression becomes
1+(~0)= 2
But the answer that I got was 0. I know it is some thing to do with the fact that ~ is right associative but I cannot put my finger on it
I think there is some problem with your formatting. Write your if else like this
if( condition )
{
// Some code
}
else
{
// Some code
}
This is how I wrote the while loop in my editor
while (input != EOF)
{
if(sec > 9 )
{
excess_min = (min) + (sec * 0.01);
}
else
{
excess_min = (min) + (sec * 0.1);
}
// JOG_min = JOG_minutes(excess_min);
// JOG_min_total = JOG_min + JOG_min_total;
input = scanf("%f %f", &min, &sec);
}
and this compiled perfectly
Use fread to read from the file . Check out this link
http://irc.essex.ac.uk/www.iota-six.co.uk/c/i2_feof_fgets_fread.asp
while(check till end of file)
{
/*
fread will put the data in array1
After that use string copy to move the data to character pointer array...lets say array2
*/
}
There are many mistakes in your code
1. When you wanted to remove the number 6, you took the ASCII value of 6 ie 54 but forgot to subtract the ASCII value of 0. Hence your i variable became too big.
2. When you read the number 6 you have only 5 characters to read. So the for loop should not be <= but just <
3. When you start the second iteration, the value of c is 4 but the value of m is already greater than 4 so it does not enter the loop
4. Ensure that after you have finished reading the frame , the value of has decremented upto the correct value
5. After you have made all these changes , remove scanf and use fgets instead
Try coders for hire, students of fortune.
They may be more amenable to your request
Open the file
Store the contents of the file, one line at a time into an array- Possibly an array of character pointers
Sort the character pointer array
Oh ...ok got it
Hello People... I found this sample code online
int main(int argc, char* argv[])
{
struct bitfield
{
signed int a:3;
unsigned int b:13;
unsigned int c:1;
};
struct bitfield bit1={2,14,1};
printf("%d",sizeof(bit1));
return 1;
}
I compiled this code on linux with gcc compiler and the output I got for this is 4. Can any one explain why this so ?
Another option could be to use command line arguments
Also in the main function. You have to declare variable before using them .....
Can you explain the question more clearly.
Also if you can, post the sample input and output
There is no restriction as such. You can have the scanf where ever you want
You do not need the fscanf function. At the point when end of the file is reached, the code will not enter the while loop. Consequently you can do away with the if on line 21
Also you instead of incrementing the count by 1 and then using the mod operation, another way could be to toggle the count values between 0 and 1 using if conditions
int main(int argc, char* argv[])
{
int a=3,b=2,c;
a=a==b==0;
printf("%d\n",a);
return 1;
}
I cannot understand why this code gives 1 as the answer.
According to me , this should be the order of evaluation
b==0, which gives false & the expression becomes a=a==0
this should again give false & the expression should become
a=0
thereby the value of a should be 0
Can you post the output that you are getting. This is the output that I am getting when I run your code
timberlake {~} > ./test1
9 9 1 1 1 8 8 8 8 2 2 7 7 3 3 3 4196247 9 9 8
timberlake {~} >
The last 4 values are junk. But the main point is I think no values from array B have been skipped
I want to see your print function . If it is some thing of this sort
for(i=0;i<20;i++)
printf("%d\n",arr3[i]);
Then you will get junk characters in the ending. The reason for this is that there are less than 20 numbers in arr 3.
current row + 1, current row -1, current col +1, current col -1
But you will have to take care of the boundary conditions
Ok.... I think I have finally understood the question..
You could try to do some thing of this sort..
For each row
For each column
When you see a non zero number say a, check its 4 sides and push the positions of all the non zero numbers in a stack. Then pop the first element say b and push the positions of all of its non zero neighbors onto the same stack. Not the stack contains the positions of non zero neighbors of the both a and b. If you go on this way, by the time, the stack is empty you would have finished the first square
I am sorry I cannot understand the problem.....
In one of your earlier posts you had pasted the matrix. For that matrix what is the output ?
while(line[k] != '\0')
{
argv[i] = (char *)malloc(sizeof(char) * 11);
/*
while(line[k] != ' ')
This should be
while(line[k]!= ' '&& line[k]!='\0')
If you give the command of the type "/bin/ls , your loop keeps on
searching till it find the space character
*/
{
argv[i][j] = line[k];
k ++;
j ++;
}
argv[i][j] = '\0';
j = 0;
k ++;
i ++;
}
execve(argv[0], argv, envp);
return 0;
}
Also why are you using the argv array to store the parameters. Why not make a separate array ?
Do you have sample input and out put ? Can you post that ?
I am not SURE.... but it is very unlikely...
Well from WHAT I KNOW, most modern operating systems do not use SJFS.
SJFS is more common in the batch computing days when the user had to explicitly submit his jobs. In such a scenario when the user submitted his job he was required to estimate the run time also
Most of the current operating systems use prioritized round robin scheme. In this all processes start at the same priority. Then if the process used the entire time slot and does not finish, it is assumed that this is a batch job and the priority of this process decreases. Similarly if the process uses a part of it allotted time and then tell the OS that it needs IO, the process is assumed to be interactive and its priority is increased.
The cin part enables the user to input values which can then be stored in the variables a, b, c.
Think of a,b, c as 3 containers. When you declare them
int a,b,c;
You are telling the computer that at some point some one will store a value in this container. When you use cin statements, the users provides the values which are then stored in the containers.
I hope this makes it clearer.
There is a mistake on line 8.
while(i++<5) // the first value you are dealing with is 1 not 0
result[i]=x[i]-y[i];
The first struct you have defined is wrong. The second struct you have defined is correct. So make this change
It is not necessary that the main function return an int .
void main is also fine
Got it ... Thanks guys
Hi All,
Today I came across a peculiar piece of code.
int main(int argc, char* argv[])
{
printf("%lf\n",(0.99-0.90-0.09));
}
According to me the output for this code should be 0, but to my surprise when I ran this code the answer that I got was -0.000.
I cannot understand the reason for the minus sign