C :: How To Print Out Structures Using Loops
Jun 23, 2013How to print out these structures using loops.. printing out these outputs using loops..
View 5 RepliesHow to print out these structures using loops.. printing out these outputs using loops..
View 5 RepliesI have a structure for 4 divisions, and can't figure out how to loop through it.
So I ended up writing everything for times.
If I had 500 divisions I wouldn't be able to get away with it!
How can I have the structure in array form so I can fill it via looping?
/*Headers*/
#include <iostream>//needed 4 input/output
#include <iomanip>//needed to round
#include <string>//needed 4 strings
using namespace std;//global namespace to avoid name clash
struct division {
[code].....
i am trying to write a program to get a number from user and then print even numbers as well as 0,2,4 and so on.what changes i should do ?
PHP Code:
# include <iostream>
using namespace std;
int main () {
int n,i;
cout << " Enter number ";
cin >>n;
i = 0;
while ( i%2 == n) {
cout <<i<<" ";
i = i+1;
} system ("PAUSE");
return 0;
}
I'm trying to make a percentage counter inside a loop, printing each completed percent of the loop as it goes. I've managed to write such code, but when I run it the percentage output breaks down (becomes negative!) for large loops. I have an example below.
Code:
#include <iostream>
#include <sstream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
using namespace std ;
int main() {
[code].....
Compiling and running the above yields the output:
Code:
...1% ...2% ...3% ...4% ...5% ...6% ...7% ...8% ...9% ...10% ...11% ...12% ...13% ...14% ...15% ...16% ...17% ...18% ...19% ...20% ...21% ...-21% ...-20% ...-19% ...-18% ...-17% ...-16% ...-15% ...-14% ...-13% ...-12% ...-11% ...-10% ...-9% ...-8% ...-7% ...-6% ...-5% ...-4% ...-3% ...-2% ...-1% ...0% ...1% ...2% ...3% ...4% ...5% ...6% ...7% ...8% ...9% ...10% ...11% ...12% ...13% ...14% ...15% ...16% ...17% ...18% ...19% ...20% ...21% ...-21% ...-20% ...-19% ...-18% ...-17% ...-16% ...-15% ...-14% ...-13% ...-12% ...-11% ...-10% ...-9% ...-8% ...-7% ...-6% ...-5% ...-4% ...-3% ...-2% ...-1% ...0% ...1% ...2% ...3% ...4% ...5% ...6% ...7% ...8% ...9% ...10% ...11% ...12% ...13% ...14%...100%
If I change the value of N to 1e7 instead of 1e8, the output is correct however!
Code:
...1% ...2% ...3% ...4% ...5% ...6% ...7% ...8% ...9% ...10% ...11% ...12% ...13% ...14% ...15% ...16% ...17% ...18% ...19% ...20% ...21% ...22% ...23% ...24% ...25% ...26% ...27% ...28% ...29% ...30% ...31% ...32% ...33% ...34% ...35% ...36% ...37% ...38% ...39% ...40% ...41% ...42% ...43% ...44% ...45% ...46% ...47% ...48% ...49% ...50% ...51% ...52% ...53% ...54% ...55% ...56% ...57% ...58% ...59% ...60% ...61% ...62% ...63% ...64% ...65% ...66% ...67% ...68% ...69% ...70% ...71% ...72% ...73% ...74% ...75% ...76% ...77% ...78% ...79% ...80% ...81% ...82% ...83% ...84% ...85% ...86% ...87% ...88% ...89% ...90% ...91% ...92% ...93% ...94% ...95% ...96% ...97% ...98% ...99%...100%
I am trying to read from a data file that has input as :
12.0, 11, 123
14.0, 12.1, 3
And I want the program to read the data from the file and then make it into an array of structures and then print that array afterwards.
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_INPUT 1000
int n =0;
int i = 0;
typedef struct {
double x[MAX_INPUT];
[Code] .....
The program when run gives the following output:
Ishtiaque-Mohammed-Khans-MacBook-Pro:Comp20005 IshtiaqueMKhan$ gcc -Wall -ansi -o ProjectB ProjectB.c
ProjectB.c: In function "main":
ProjectB.c:59: error: incompatible type for argument 1 of "print_array"
I am making a program where the user enters numbers into an array and then a number,x. The array is sorted, then x is inserted into the appropriate place. I wrote my selection sort
Code:
void Sort(int ary[], int size)
{
int temp;
int smallest;
int current;
int move;
}
[code]....
put it wont print the numbers sorted when I use my print function, just the unsorted numbers.
is there a to use printf to print certain number of blank spaces, where the number is derived from the output of a function?
for(m=low; m<=high;m++)
{
printf("t=%2d %'f(m)-1's %3c", m, ' ',*);
}
This is suppose to output
t=-3 *
t=-2 *
t=-1
.
.
.
I this notation:
for (; *strings[i]; i++)
the same as:
do {
i++
} while(*strings[i]);
?
Ok, for my Sunday homework I have to find the next day of the year using structures. Now, I can get the structure to read the entered date (tested that with a simple scanf to printf entered data), but I need to get it to be able to tell that it is the end of the month and print out the next month when necessary. Here is what I got so far:
Code:
//Cameron Taylor
#include <stdio.h>
struct Date{
int month;
int day;
int year;
[Code] ....
As you can see, I am trying to use an array for the max day of each month (excluding Leap Year for now).
Where I use the entered month to = 'i'. Then I use 'i' as the position in the array to see if next.day is = to the max day of that position of the array.
I am having no compiler issues with GCC using -w or -Wall, but after entering the current day the program stops working and doesn't go beyond the input.
commiedic@localhost Project18$ gcc -o -Wall Exercise4.c
commiedic@localhost Project18$ gcc -o -w Exercise4.c
commiedic@localhost Project18$ gcc -w Exercise4.c
commiedic@localhost Project18$ make Exercise4
make: `Exercise4' is up to date.
[Code] .....
I have to do a project for Programming and he said in quotes
"Programming exercise 10.6: the programming project is as described, except do not represent the points using structures.
Assume, as known, the ten (x,y)-values corresponding to x=0.0, 0.1, 0.3, 0.45 ..etc and use y-values from the
function xsin(x); as in (x,y)=(x, xsin(x)) = (0.3, 0.3sin(0.3)).
Use value n+1=10, so the LaGrange polynomial will be order = 9."
What does it mean to not represent the points using structures??
I have an assignment that calls for a C program using a for loop AND a while loop that will receive an integer (called daNumba) and double it -- Using the integer the program will call the sumFor function and then the sumWhile function. These functions will both sum the values from daNumba to (daNumba * 2) ifdaNumba is positive. If daNumba is not positive it will add the values from (daNumba*2) to daNumba. Both functions will receive the value of daNumba and return a summed value. The only difference between the 2 functions is that sumFor will only use for loops and sumWhile will only use while loops. We are not to use arrays.
The program compiles without error. So far my while loop works for positive integers, but not with a negative integer (I have it commented out) I cannot get the for loop to work properly This is what I have so far -- I am stuck....
Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main () {
[Code] ....
See code below:
#include <stdlib.h>
#include <stdio.h>
int main ( int argc, char *argv[] ) {
int P[150] = {}, i, j;
for ( i = 2; i <= 150; i++ ) {
[Code] .....
Using gdb, I noticed that the variable j keep going back to initial value after the interior for loop condition returns false. Why doesn't this for loop terminate right away?
I've just started learning the C language, and I'm stuck on something that is probably quite simple.how to get IF statements working within WHILE loops. My code is below. I am trying to write a program to count the number of words in a sentence, and obviously stop counting after a full stop has been entered. I created a variable called 'spaces' which will increase by one after the user enters a space. However, when the IF statement is in place, the loop never terminates, even if I enter a full stop. When I delete the IF statement, the loop functions correctly.
Code:
#include <stdio.h>
int main()
{
char x;
char a;
char y;
int spaces = 0;
}
[code]....
What output would you expect from this program?" The output was not what I expected. I've psuedo-coded this out and I'm still missing something.
Code:
#include <stdio.h>
int main () {
int numbers[10] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int i, j;
}
[code]....
The output: Code: 1 1 2 4 8 16 32 64 128 256 So when I look at this first loop I see that j = 0, which is less than 10, so then the program statement should commence, which is another for loop. So in this inner for loop I see that i = 0, which is not less than j, so this loop should terminate. Then the value of j increments by 1 and the first go around of the loop has completed.
Now I see that j = 1, so this is less than 10, and the inner for loop commences once again. This time though, i actually is less than j, so numbers[1] = numbers[1] + numbers [0], or numbers[1] = 0 + 1. Now the value of i is incremented by 1 and the first go around of this inner loop has completed. Then the value of j increments by 1 and another go around of that loop has completed.
So now j = 2, i = 1, and numbers[2] ( which is 0 ) = numbers[2] + numbers[1], or numbers[2] = 0 + 1. I was expecting the output to be an array full of 1's. However this is not the case..
I need to make a for loop without: using the math functions like pow, sqrt, etc. or an if-statement.I can only use the basic arithmetic functions like +,-,*, and.The for loop needs to display: 1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192 as the result
How do I make it so my loop works? This is what I have and it doesn't work...and I have no clue how to continue...
k = 0; //k is an int and I have declared it in the beginning of my code
for( k = 0; k < 13; k++ ) {
printf ( "%d, ",k );
k = k * 2;
}
printf("%d", x);
I just want to say that I just started learning the language and this is my very first shot at writing a simple program that is not "Hello World" So I recently learnt the basics of the if statement and how to loop in a console application, here's what it looks like:
namespace Testing_IF_Statement
{
class Program
{
[Code].....
And again, this is just what I wrote in a few minutes without putting any thought into it.
Questions:
1)I have heard that the way I'm looping by using the goto statement is considered quite ancient, why is this and how else could I loop the program?
2)I declared a as an integer and asked the user to type 1,2,3,4 to perform mathematical functions, but when I tried declaring a as a string and searching if a == "PLUS" etc it would throw an error. Is there any mistake in how I approached this?
3)I know this is a bit premature to ask, but instead of writing _________ in the console to separate the loop is there any professional way to add a separator?
4)In general how can I improve the code?
im trying to make a program which will tell which key i pressed and then it will print in msgbox, but i cant figure out how to read the key in loops, like i tried getasynkey (imported to my c# console app) with value around -32676 or -32767 i ( i found it on internet) and it only shows the key once, i want to do like while my key is pressed, then it will spam my console with key pressed, is there any way to do it?
View 6 Replies View RelatedParts of this program are missing. The last few lines are confusing, since the variable 'a' gets incremented then decremented. But there are no loops. I understand that the value of 'a' is passed to 'c' before 'a' is changed in both cases.
But where, and when, do the changes take place? Is the decrement ever processed? Is there a better way to write these lines?
Code:
main(){ int a = 21;int b = 10;int c ;
c = a++;
cout << "Line 6 - Value of c is :" << c << endl ;
c = a--;
cout << "Line 7 - Value of c is :" << c << endl ;
return 0;}
i am trying to write program that prints out full lyrics to 99 bear song. Hoping for explanation or anything
The output would have to look like:
99 bottles of beer on the wall, 99 bottles of beer.
Take one down and pass it around, 98 bottles of beer on the wall.
My try below:
Code: #include <iostream>
using namespace std;
int main()
{
for(int i=99; i>=0 ;i--)
cout << i <<""<< " bottles of beer on the wall, "<<i<<" bottles of beer."<<endl;
for(int j=98; j>=0 ;j--){
cout <<endl;
cout << "Take one down and pass it around," <<j<<" bottles of beer on the wall."<<endl;
}
return 0;
}
I'm trying to make a program that prints a rectangle using "*" asterisks. I am only allowed to use "for loops" and I simply cannot get this to work properly. Here is my code so far...
Code:
int main(void)
{
//RectangleSize represents the area of the rectangle(width*length)
int length, width, RectangleSize;
[Code].....
Howi can made nested loops?
Code:
for (yax=0; yax<10; yax=yax+1) {
for (xax=0; xax<100; xax=xax+1) {
printf("%d
",yax);
}
}
way what i tired dont work. or maybe works but why this prints only zeros ?
A user is supposed to enter student name, id, and grade then sort them by name, id, or grade. I managed to get it to sort by name and id correctly, but not by grade.
Code] .....
#include <string.h>
#include <stdio.h>
struct student{
int student_id;
char name[30];
char grade[15];
[Code] ....
Using loops that would generate the same results. I need to use a loop that would make the syntax less lengthy ....
Code:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
float check(float s);
void load(float*);
[Code] .....
getting my program to print a hexagon made of asterisks. I can get the program to print a solid hexagon, but I need a hollow hexagon.
Code:
#include<stdio.h>
int main(void) {
int length, i=0, j=0, k, l;
[Code].....
How do I store pointers to a struct in an array ? I am using sprintf to concatenate some values together and then output it to an array in its 1st argument. A portion of my code is shown below.
Code:
int count = 0;
struct addx {
int a;
[Code].....
I am bit confused in passing the structure to a function. I made a small code and it doesn't build !
Code:
#include<stdio.h>
struct Compute_off_Time(struct WHATTIME);
typedef union _Time {
unsigned long int Value;
[Code] ....